{"record":{"id":"23bcb38b138cecde","repo":"lepoco/wpfui","slug":"nameof-pageservice-getpage-returned-null","errorCode":null,"errorMessage":"{nameof(_pageService.GetPage)} returned null","messagePattern":"(.+?) returned null","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs","lineNumber":329,"sourceCode":"        {\n            System.Diagnostics.Debug.WriteLine(\n                $\"Getting {targetPageType} from cache using IServiceProvider.\"\n            );\n\n            return _serviceProvider.GetService(targetPageType)\n                ?? throw new InvalidOperationException(\n                    $\"{nameof(_serviceProvider.GetService)} returned null\"\n                );\n        }\n\n        if (_pageService is not null)\n        {\n            System.Diagnostics.Debug.WriteLine(\n                $\"Getting {targetPageType} from cache using INavigationViewPageProvider.\"\n            );\n\n            return _pageService.GetPage(targetPageType)\n                ?? throw new InvalidOperationException($\"{nameof(_pageService.GetPage)} returned null\");\n        }\n\n        System.Diagnostics.Debug.WriteLine($\"Getting {targetPageType} from cache using reflection.\");\n\n        return NavigationViewActivator.CreateInstance(targetPageType)\n            ?? throw new InvalidOperationException(\"Failed to create instance of the page\");\n    }\n\n    private void ApplyAttachedProperties(INavigationViewItem viewItem, object pageInstance)\n    {\n        if (pageInstance is FrameworkElement frameworkElement)\n        {\n            // Store the association between page and navigation item\n            PageToNavigationItemDictionary[frameworkElement] = viewItem;\n\n            // Apply HeaderContent if already available\n            if (GetHeaderContent(frameworkElement) is { } headerContent)\n            {","sourceCodeStart":311,"sourceCodeEnd":347,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs#L311-L347","documentation":"Thrown by NavigationView.GetPageInstanceFromCache when no IServiceProvider is set but an INavigationViewPageProvider is, and its GetPage(targetPageType) returns null while filling the cache. Cache-layer twin of error 36; same root cause (page provider does not handle the type) surfaced from the cache factory.","triggerScenarios":"NavigationCacheMode enabled for an item, no service provider configured, the INavigationViewPageProvider.GetPage returns null for the target page type when the cache asks for an instance.","commonSituations":"Custom page provider missing an entry for a page that is now cached; provider's switch/dict out of date after adding a new page; provider returns null on a transient error.","solutions":["Make GetPage return a non-null instance for every TargetPageType that may be cached.","Keep the provider's page map in sync with the NavigationViewItems declared in XAML/view-model.","Add logging in GetPage to identify which type returns null.","Alternatively, register pages with an IServiceProvider so the cache uses DI instead."],"exampleFix":"// before\npublic object? GetPage(Type t) =>\n    t == typeof(HomePage) ? new HomePage() : null; // SettingsPage -> throws\n\n// after\npublic object? GetPage(Type t) => t switch {\n    typeof(HomePage) => new HomePage(),\n    typeof(SettingsPage) => new SettingsPage(),\n    _ => null\n};","handlingStrategy":"validation","validationCode":"if (_pageService.GetPage(targetPageType) is null)\n    throw new InvalidOperationException(\n        $\"Page provider returned null for {targetPageType}; cannot fill cache.\");","typeGuard":"static bool IsPageKnown(INavigationViewPageProvider p, Type t) => p.GetPage(t) is not null;","tryCatchPattern":null,"preventionTips":["Keep the page provider's map in sync with declared NavigationViewItems.","Cover every page type that may be cached in GetPage.","Log inside GetPage to identify which type yields null."],"tags":["wpf","navigationview","page-provider","cache","configuration"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}