PrismLibrary/Prism · error · InvalidOperationException

Cannot create navigation target

Error message

Cannot create navigation target '{0}'.

What it means

RegionNavigationContentLoader.CreateNewRegionItem wraps any non-resolution failure while instantiating a view for a navigation contract in InvalidOperationException('Cannot create navigation target X'), preserving the cause as InnerException. ContainerResolutionException is rethrown untouched; everything else becomes this error.

Solutions

  1. Inspect the InnerException — it contains the actual construction failure
  2. Fix the view's constructor so it does not throw (register all injected dependencies in the container)
  3. Verify the view type still exists and matches the registered navigation contract name
  4. Test resolving the view directly from the container to reproduce the construction error

Example fix

// before
public partial class MyPage
{
    public MyPage(MyService svc) // svc not registered -> construction throws
    { InitializeComponent(); }
}
// after
// in App:
containerRegistry.RegisterSingleton<MyService>();
// and/or use parameterless ctor + ViewModel injection
public partial class MyPage() { InitializeComponent(); }
Defensive patterns

Strategy: try-catch

Validate before calling

var viewType = locatorRegistry.GetTypeForNavigation(contract);
if (viewType is null) throw new InvalidOperationException($"Unknown navigation target {contract}");

Try / catch

try { regionManager.RequestNavigate(contract); }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Cannot create navigation target"))
{ logger.Error(ex.InnerException, "Navigation target '{0}' failed to construct", contract); }

Prevention

When it happens

Trigger: LoadContent -> CreateNewRegionItem fails while constructing the view for contract X: constructor threw, static initializer failed, view could not be instantiated via the container in an unexpected way (resolution itself failing surfaces as ContainerResolutionException instead).

Common situations: View constructor throwing (missing service in constructor chain, XAML load failure inside the page's InitializeComponent); type exists in container but its creation crashes; assembly loading issues after renaming view classes.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/85d112f857b29478. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/Navigation/RegionNavigationContentLoader.cs:77

    /// <returns>An instance of an item to put into the <see cref="IRegion"/>.</returns>
    protected virtual object CreateNewRegionItem(string candidateTargetContract, IRegion region)
    {
        try
        {
            var registry = region.Container().Resolve<IRegionNavigationRegistry>();
            return registry.CreateView(region.Container(), candidateTargetContract);
        }
        catch (KeyNotFoundException)
        {
            throw;
        }
        catch (ContainerResolutionException)
        {
            throw;
        }
        catch (Exception e)
        {
            throw new InvalidOperationException(
                string.Format(CultureInfo.CurrentCulture, Resources.CannotCreateNavigationTarget, candidateTargetContract),
                e);
        }
    }

    /// <summary>
    /// Returns the candidate TargetContract based on the <see cref="NavigationContext"/>.
    /// </summary>
    /// <param name="navigationContext">The navigation contract.</param>
    /// <returns>The candidate contract to seek within the <see cref="IRegion"/> and to use, if not found, when resolving from the container.</returns>
    protected virtual string GetContractFromNavigationContext(NavigationContext navigationContext)
    {
        ArgumentNullException.ThrowIfNull(navigationContext);

        var candidateTargetContract = UriParsingHelper.EnsureAbsolute(navigationContext.Uri).AbsolutePath;
        candidateTargetContract = candidateTargetContract.TrimStart('/');
        return Uri.UnescapeDataString(candidateTargetContract);
    }

View on GitHub (pinned to 358118cd64)