lepoco/wpfui · error · InvalidOperationException
Unable to get or create instance of {viewItem.TargetPageType
Error message
Unable to get or create instance of {viewItem.TargetPageType} from cache. What it means
Thrown when neither IServiceProvider nor INavigationViewPageProvider is configured, and the cache's Remember factory (which falls through to GetPageInstanceFromCache) also returns null. This is the last-resort resolution path: with no DI and no page provider, the cache must produce an instance, and a null result means even the cache factory could not create one — typically because Activator/CreateInstance failed or returned null.
Source
Thrown at src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs:296
?? throw new InvalidOperationException(
$"{nameof(_serviceProvider)}.{nameof(_serviceProvider.GetService)} returned null for type {viewItem.TargetPageType}."
);
}
if (_pageService is not null)
{
return _pageService.GetPage(viewItem.TargetPageType)
?? throw new InvalidOperationException(
$"{nameof(_pageService)}.{nameof(_pageService.GetPage)} returned null for type {viewItem.TargetPageType}."
);
}
return _cache.Remember(
viewItem.TargetPageType,
viewItem.NavigationCacheMode,
ComputeCachedNavigationInstance
)
?? throw new InvalidOperationException(
$"Unable to get or create instance of {viewItem.TargetPageType} from cache."
);
object? ComputeCachedNavigationInstance() => GetPageInstanceFromCache(viewItem.TargetPageType);
}
private object? GetPageInstanceFromCache(Type? targetPageType)
{
if (targetPageType is null)
{
return default;
}
if (_serviceProvider is not null)
{
System.Diagnostics.Debug.WriteLine(
$"Getting {targetPageType} from cache using IServiceProvider."
);View on GitHub (pinned to ffebacd610)
Solutions
- Ensure TargetPageType is a concrete class with a public parameterless constructor.
- If the page needs dependencies, configure an IServiceProvider (so error 35's path is used instead) or an INavigationViewPageProvider.
- If the type legitimately cannot be default-constructed, supply a page provider that builds it.
- Check the inner exception / debug output from NavigationViewActivator to see why instantiation failed.
Example fix
// before
public abstract class SettingsPage : Page {} // throws on navigate
// after
public class SettingsPage : Page
{
public SettingsPage() { InitializeComponent(); }
} Defensive patterns
Strategy: validation
Validate before calling
var t = viewItem.TargetPageType;
if (t is null || t.IsAbstract || t.IsInterface || t.GetConstructor(Type.EmptyTypes) is null)
throw new InvalidOperationException(
$"{t} must be a concrete type with a public parameterless constructor, or supply a page provider."); Type guard
static bool IsInstantiable(Type? t) =>
t is not null && !t.IsAbstract && !t.IsInterface &&
t.GetConstructor(Type.EmptyTypes) is not null; Prevention
- Point TargetPageType only at concrete, parameterless-constructible classes.
- If the page has dependencies, supply an IServiceProvider or page provider.
- Check NavigationViewActivator debug output when instantiation fails.
When it happens
Trigger: Navigating to a page with no service provider and no page service, where the cache's ComputeCachedNavigationInstance (-> GetPageInstanceFromCache) returns null — e.g. the target type is abstract, an interface, lacks a parameterless constructor, or NavigationViewActivator.CreateInstance returned null.
Common situations: TargetPageType points to an interface or abstract class; the page type has no public parameterless constructor; the type is a generic open definition; an exception inside the activator was swallowed and returned null.
Related errors
- The {nameof(viewItem)}.{nameof(viewItem.TargetPageType)} pro
- {nameof(_serviceProvider)}.{nameof(_serviceProvider.GetServi
- {nameof(_pageService)}.{nameof(_pageService.GetPage)} return
- {nameof(_serviceProvider.GetService)} returned null
- {nameof(_pageService.GetPage)} returned null
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/083130618228e2ea.
Report an issue: GitHub.