lepoco/wpfui · error · InvalidOperationException
The {nameof(viewItem)}.{nameof(viewItem.TargetPageType)} pro
Error message
The {nameof(viewItem)}.{nameof(viewItem.TargetPageType)} property cannot be null. What it means
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.
Source
Thrown at src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs:270
Journal.Add(viewItem.Id);
_currentIndexInJournal++;
SetCurrentValue(IsBackEnabledProperty, CanGoBack);
Debug.WriteLineIf(EnableDebugMessages, $"JOURNAL INDEX {_currentIndexInJournal}");
if (Journal.Count > 0)
{
Debug.WriteLineIf(EnableDebugMessages, $"JOURNAL LAST ELEMENT {Journal[^1]}");
}
}
private object GetNavigationItemInstance(INavigationViewItem viewItem)
{
if (viewItem.TargetPageType is null)
{
throw new InvalidOperationException(
$"The {nameof(viewItem)}.{nameof(viewItem.TargetPageType)} property cannot be null."
);
}
if (_serviceProvider is not null)
{
return _serviceProvider.GetService(viewItem.TargetPageType)
?? 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}."
);View on GitHub (pinned to ffebacd610)
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.
Example fix
<!-- before -->
<ui:NavigationViewItem Content="Settings"/> <!-- throws when selected -->
<!-- after -->
<ui:NavigationViewItem Content="Settings" TargetPageType="{x:Type pages:SettingsPage}"/> Defensive patterns
Strategy: validation
Validate before calling
if (viewItem.TargetPageType is null)
throw new InvalidOperationException(
$"NavigationViewItem '{viewItem.Content}' has no TargetPageType."); Type guard
static bool IsNavigable(INavigationViewItem i) => i.TargetPageType is not null;
Prevention
- Set TargetPageType on every selectable NavigationViewItem.
- Make header/separator items non-selectable.
- Validate the item in DEBUG before calling Navigate.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- DialogHost was not set
- {nameof(_serviceProvider)}.{nameof(_serviceProvider.GetServi
- {nameof(_pageService)}.{nameof(_pageService.GetPage)} return
- Unable to get or create instance of {viewItem.TargetPageType
- {nameof(_serviceProvider.GetService)} returned null
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/9af97ddbb0c2ad03.
Report an issue: GitHub.