lepoco/wpfui · error · InvalidOperationException

{nameof(_pageService)}.{nameof(_pageService.GetPage)} return

Error message

{nameof(_pageService)}.{nameof(_pageService.GetPage)} returned null for type {viewItem.TargetPageType}.

What it means

Thrown by NavigationView.GetNavigationItemInstance when no IServiceProvider is configured but an INavigationViewPageProvider (the _pageService) is, and its GetPage(TargetPageType) returns null. The page provider is the second resolution strategy; a null means the provider does not know how to build the requested page.

Source

Thrown at src/Wpf.Ui/Controls/NavigationView/NavigationView.Navigation.cs:286

        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}."
                );
        }

        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)
    {

View on GitHub (pinned to ffebacd610)

Solutions

  1. Implement GetPage in your INavigationViewPageProvider to return a non-null instance for every navigable TargetPageType.
  2. If the provider uses a dictionary, ensure all target page types are keys.
  3. Log inside GetPage to diagnose why null is returned.
  4. Alternatively, register the page with an IServiceProvider instead and let NavigationView resolve it.

Example fix

// before
public object? GetPage(Type pageType) => null; // throws

// after
public object? GetPage(Type pageType) =>
    pageType == typeof(SettingsPage) ? new SettingsPage() : null;
Defensive patterns

Strategy: validation

Validate before calling

if (_pageService.GetPage(viewItem.TargetPageType) is null)
    throw new InvalidOperationException(
        $"Page provider returned null for {viewItem.TargetPageType}.");

Type guard

static bool IsPageKnown(INavigationViewPageProvider p, Type t) => p.GetPage(t) is not null;

Prevention

When it happens

Trigger: Navigating to a NavigationViewItem whose TargetPageType is not handled by the custom INavigationViewPageProvider.GetPage implementation; the provider's dictionary/switch is missing the page; the provider returned null due to an internal error.

Common situations: A custom page provider that maintains an explicit map and the page was not added; provider that throws-and-swallows internally returning null; page registered under a key/type mismatch.

Related errors


AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13). Data as JSON: /api/errors/87edf8cd01d16fbf. Report an issue: GitHub.