lepoco/wpfui · error · NavigationException

{typeof(TPage)} page not found.

Error message

{typeof(TPage)} page not found.

What it means

GetRequiredPage<TPage> is an extension method on INavigationViewPageProvider that calls GetPage(typeof(TPage)) and throws NavigationException if it returns null. It is the strict variant of GetPage for cases where the caller cannot proceed without the page. A null result means the provider has no mapping/registration for the requested page type.

Source

Thrown at src/Wpf.Ui.Abstractions/NavigationViewPageProviderExtensions.cs:37

    public static TPage? GetPage<TPage>(this INavigationViewPageProvider navigationViewPageProvider)
        where TPage : class
    {
        return navigationViewPageProvider.GetPage(typeof(TPage)) as TPage;
    }

    /// <summary>
    /// Retrieves a page of the specified type from the page service.
    /// Throws a NavigationException if the page is not found.
    /// </summary>
    /// <typeparam name="TPage">The type of the page to retrieve.</typeparam>
    /// <param name="navigationViewPageProvider">The page service instance.</param>
    /// <returns>An instance of the specified page type.</returns>
    /// <exception cref="NavigationException">Thrown when the specified page type is not found.</exception>
    public static TPage GetRequiredPage<TPage>(this INavigationViewPageProvider navigationViewPageProvider)
        where TPage : class
    {
        return navigationViewPageProvider.GetPage(typeof(TPage)) as TPage
            ?? throw new NavigationException($"{typeof(TPage)} page not found.");
    }
}

View on GitHub (pinned to ffebacd610)

Solutions

  1. Register the page type with your INavigationViewPageProvider / DI container so GetPage returns a non-null instance.
  2. Ensure you request the exact type the provider resolves (concrete class, not an interface or base).
  3. Use the non-throwing GetPage when absence is expected, and only call GetRequiredPage when the page is mandatory.

Example fix

// before
public class PageService : INavigationViewPageProvider
{
    public object? GetPage(Type t) => null; // nothing registered
}
var page = provider.GetRequiredPage<DashboardPage>(); // throws

// after
public class PageService(IServiceProvider sp) : INavigationViewPageProvider
{
    public object? GetPage(Type t) => sp.GetService(t);
}
// plus: services.AddTransient<DashboardPage>();
Defensive patterns

Strategy: try-catch

Validate before calling

var page = provider.GetPage(typeof(TPage));
if (page is null)
{
    throw new NavigationException($"{typeof(TPage)} not registered in the page provider.");
}

Type guard

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

Try / catch

try { var page = provider.GetRequiredPage<TPage>(); }
catch (NavigationException ex)
{
    _logger.LogError(ex, "Page {Page} not found; ensure it is registered.", typeof(TPage));
    return;
}

Prevention

When it happens

Trigger: Calling GetRequiredPage<TPage>() for a TPage that the page provider does not know how to build - because it was never registered, the DI container cannot resolve it, or the provider's dictionary lacks the mapping.

Common situations: Forgetting to register a page in a custom INavigationViewPageProvider; the page type requested differs from the registered type (e.g. interface vs concrete); the underlying service returns null due to a missing DI registration.

Related errors


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