{"record":{"id":"083130618228e2ea","repo":"lepoco/wpfui","slug":"unable-to-get-or-create-instance-of-viewitem-targ","errorCode":null,"errorMessage":"Unable to get or create instance of {viewItem.TargetPageType} from cache.","messagePattern":"Unable to get or create instance of (.+?) from cache\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs","lineNumber":296,"sourceCode":"                ?? throw new InvalidOperationException(\n                    $\"{nameof(_serviceProvider)}.{nameof(_serviceProvider.GetService)} returned null for type {viewItem.TargetPageType}.\"\n                );\n        }\n\n        if (_pageService is not null)\n        {\n            return _pageService.GetPage(viewItem.TargetPageType)\n                ?? throw new InvalidOperationException(\n                    $\"{nameof(_pageService)}.{nameof(_pageService.GetPage)} returned null for type {viewItem.TargetPageType}.\"\n                );\n        }\n\n        return _cache.Remember(\n                viewItem.TargetPageType,\n                viewItem.NavigationCacheMode,\n                ComputeCachedNavigationInstance\n            )\n            ?? throw new InvalidOperationException(\n                $\"Unable to get or create instance of {viewItem.TargetPageType} from cache.\"\n            );\n\n        object? ComputeCachedNavigationInstance() => GetPageInstanceFromCache(viewItem.TargetPageType);\n    }\n\n    private object? GetPageInstanceFromCache(Type? targetPageType)\n    {\n        if (targetPageType is null)\n        {\n            return default;\n        }\n\n        if (_serviceProvider is not null)\n        {\n            System.Diagnostics.Debug.WriteLine(\n                $\"Getting {targetPageType} from cache using IServiceProvider.\"\n            );","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs#L278-L314","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic abstract class SettingsPage : Page {} // throws on navigate\n\n// after\npublic class SettingsPage : Page\n{\n    public SettingsPage() { InitializeComponent(); }\n}","handlingStrategy":"validation","validationCode":"var t = viewItem.TargetPageType;\nif (t is null || t.IsAbstract || t.IsInterface || t.GetConstructor(Type.EmptyTypes) is null)\n    throw new InvalidOperationException(\n        $\"{t} must be a concrete type with a public parameterless constructor, or supply a page provider.\");","typeGuard":"static bool IsInstantiable(Type? t) =>\n    t is not null && !t.IsAbstract && !t.IsInterface &&\n    t.GetConstructor(Type.EmptyTypes) is not null;","tryCatchPattern":null,"preventionTips":["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."],"tags":["wpf","navigationview","reflection","activator","configuration"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}