lepoco/wpfui · error · InvalidOperationException
Failed to create instance of the page
Error message
Failed to create instance of the page
What it means
Thrown by NavigationView when it falls back to the reflection-based activator (NavigationViewActivator.CreateInstance) to build a page instance and that call returns null. This path is only reached when neither an IServiceProvider nor an INavigationViewPageProvider was registered with the NavigationView, so the control tries Activator-style instantiation and the resulting object is not a FrameworkElement (or the constructor returned null). The message is intentionally generic because CreateInstance hides the real reason; the InnerException/stack will point at NavigationViewActivator.
Source
Thrown at src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs:335
?? throw new InvalidOperationException(
$"{nameof(_serviceProvider.GetService)} returned null"
);
}
if (_pageService is not null)
{
System.Diagnostics.Debug.WriteLine(
$"Getting {targetPageType} from cache using INavigationViewPageProvider."
);
return _pageService.GetPage(targetPageType)
?? throw new InvalidOperationException($"{nameof(_pageService.GetPage)} returned null");
}
System.Diagnostics.Debug.WriteLine($"Getting {targetPageType} from cache using reflection.");
return NavigationViewActivator.CreateInstance(targetPageType)
?? throw new InvalidOperationException("Failed to create instance of the page");
}
private void ApplyAttachedProperties(INavigationViewItem viewItem, object pageInstance)
{
if (pageInstance is FrameworkElement frameworkElement)
{
// Store the association between page and navigation item
PageToNavigationItemDictionary[frameworkElement] = viewItem;
// Apply HeaderContent if already available
if (GetHeaderContent(frameworkElement) is { } headerContent)
{
viewItem.Content = headerContent;
UpdateBreadcrumbContents();
}
}
}
View on GitHub (pinned to ffebacd610)
Solutions
- Register an IServiceProvider or INavigationViewPageProvider with the NavigationView (or ControlsServices) so the reflection fallback is never used.
- Verify TargetPageType points to a concrete class deriving from FrameworkElement with a usable constructor.
- Ensure the page's parameterless constructor does not throw and does not return a null reference from a base init.
- If using cache/precache, set NavigationCacheMode to Disabled during debugging to surface the underlying constructor error.
Example fix
// before
var nav = new NavigationView();
// no service provider registered -> reflection path
// after
ControlsServices.Initialize(host.Services);
var nav = new NavigationView { PageService = host.Services.GetRequiredService<INavigationViewPageProvider>() }; Defensive patterns
Strategy: validation
Validate before calling
// Before navigating, ensure a page provider/service is wired and the type is concrete.
if (navView.PageService is null && ControlsServices.ControlsServiceProvider is null)
{
throw new InvalidOperationException("Register an INavigationViewPageProvider or IServiceProvider before navigating.");
}
if (!typeof(FrameworkElement).IsAssignableFrom(targetPageType))
{
throw new ArgumentException($"{targetPageType} must derive from FrameworkElement.");
} Type guard
static bool IsInstantiablePage(Type t) =>
typeof(FrameworkElement).IsAssignableFrom(t)
&& !t.IsAbstract
&& t.GetConstructor(Type.EmptyTypes) is not null; Try / catch
try { navView.Navigate(targetPageType); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Failed to create instance of the page"))
{
_logger.LogError(ex, "Reflection instantiation failed for {Type}; register a page provider.", targetPageType);
// fall back to a safe error page
} Prevention
- Always register an INavigationViewPageProvider or IServiceProvider with NavigationView so the reflection path is never used.
- Keep page constructors side-effect free so reflection instantiation does not silently fail.
- Unit-test each page's parameterless constructor in isolation.
When it happens
Trigger: A NavigationViewItem with a TargetPageType is selected while _serviceProvider and _pageService are both null on the NavigationView, AND the target page's constructor either throws, returns null, or produces an object that is not assignable to FrameworkElement. Also fired when NavigationCacheMode is enabled and the cached factory returns null.
Common situations: Hosting NavigationView without calling ControlsServices.Initialize / without injecting IServiceProvider; pages whose default constructor throws during design-time or due to missing static state; pages that are not FrameworkElement-derived; a registered page type that resolves to an interface/abstract type.
Related errors
- The {pageType} page does not have a parameterless constructo
- The `_desiredWidth` field was not found.
- The `UpdateActualWidth` method was not found.
- Failed to get the current `_desiredWidth`.
- Unable to get or create instance of {viewItem.TargetPageType
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/837d1b451eba581f.
Report an issue: GitHub.