{"record":{"id":"9af97ddbb0c2ad03","repo":"lepoco/wpfui","slug":"the-nameof-viewitem-nameof-viewitem-targetpage","errorCode":null,"errorMessage":"The {nameof(viewItem)}.{nameof(viewItem.TargetPageType)} property cannot be null.","messagePattern":"The (.+?)\\.(.+?) property cannot be null\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs","lineNumber":270,"sourceCode":"\n        Journal.Add(viewItem.Id);\n        _currentIndexInJournal++;\n\n        SetCurrentValue(IsBackEnabledProperty, CanGoBack);\n\n        Debug.WriteLineIf(EnableDebugMessages, $\"JOURNAL INDEX {_currentIndexInJournal}\");\n\n        if (Journal.Count > 0)\n        {\n            Debug.WriteLineIf(EnableDebugMessages, $\"JOURNAL LAST ELEMENT {Journal[^1]}\");\n        }\n    }\n\n    private object GetNavigationItemInstance(INavigationViewItem viewItem)\n    {\n        if (viewItem.TargetPageType is null)\n        {\n            throw new InvalidOperationException(\n                $\"The {nameof(viewItem)}.{nameof(viewItem.TargetPageType)} property cannot be null.\"\n            );\n        }\n\n        if (_serviceProvider is not null)\n        {\n            return _serviceProvider.GetService(viewItem.TargetPageType)\n                ?? 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                );","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs#L252-L288","documentation":"Thrown by NavigationView.GetNavigationItemInstance when an INavigationViewItem being navigated to has a null TargetPageType. The navigation system needs a concrete Type to instantiate or resolve the destination page; without it, navigation cannot proceed. This is a configuration error: the item was added without a target page.","triggerScenarios":"Selecting/clicking a NavigationViewItem whose TargetPageType is null; programmatic Navigate(...) with an item whose TargetPageType was never set; XAML <ui:NavigationViewItem> without a TargetPageType attribute and no other page-resolution mechanism.","commonSituations":"Adding a header-only or separator NavigationViewItem that is selectable but has no page; binding NavigationViewItem instances from a view-model that omits the page type; XAML where the TargetPageType attribute was removed during refactoring.","solutions":["Set TargetPageType on every navigable NavigationViewItem (in XAML: TargetPageType=\"{x:Type pages:MyPage}\").","For non-navigating items (headers, separators), make them non-selectable so they cannot trigger navigation.","If binding from a view-model, ensure the model exposes a non-null TargetPageType.","Validate the item before calling Navigate."],"exampleFix":"<!-- before -->\n<ui:NavigationViewItem Content=\"Settings\"/> <!-- throws when selected -->\n\n<!-- after -->\n<ui:NavigationViewItem Content=\"Settings\" TargetPageType=\"{x:Type pages:SettingsPage}\"/>","handlingStrategy":"validation","validationCode":"if (viewItem.TargetPageType is null)\n    throw new InvalidOperationException(\n        $\"NavigationViewItem '{viewItem.Content}' has no TargetPageType.\");","typeGuard":"static bool IsNavigable(INavigationViewItem i) => i.TargetPageType is not null;","tryCatchPattern":null,"preventionTips":["Set TargetPageType on every selectable NavigationViewItem.","Make header/separator items non-selectable.","Validate the item in DEBUG before calling Navigate."],"tags":["wpf","navigationview","configuration","precondition"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}