{"record":{"id":"85d112f857b29478","repo":"PrismLibrary/Prism","slug":"cannot-create-navigation-target-0","errorCode":null,"errorMessage":"Cannot create navigation target '{0}'.","messagePattern":"Cannot create navigation target '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/Navigation/RegionNavigationContentLoader.cs","lineNumber":77,"sourceCode":"    /// <returns>An instance of an item to put into the <see cref=\"IRegion\"/>.</returns>\n    protected virtual object CreateNewRegionItem(string candidateTargetContract, IRegion region)\n    {\n        try\n        {\n            var registry = region.Container().Resolve<IRegionNavigationRegistry>();\n            return registry.CreateView(region.Container(), candidateTargetContract);\n        }\n        catch (KeyNotFoundException)\n        {\n            throw;\n        }\n        catch (ContainerResolutionException)\n        {\n            throw;\n        }\n        catch (Exception e)\n        {\n            throw new InvalidOperationException(\n                string.Format(CultureInfo.CurrentCulture, Resources.CannotCreateNavigationTarget, candidateTargetContract),\n                e);\n        }\n    }\n\n    /// <summary>\n    /// Returns the candidate TargetContract based on the <see cref=\"NavigationContext\"/>.\n    /// </summary>\n    /// <param name=\"navigationContext\">The navigation contract.</param>\n    /// <returns>The candidate contract to seek within the <see cref=\"IRegion\"/> and to use, if not found, when resolving from the container.</returns>\n    protected virtual string GetContractFromNavigationContext(NavigationContext navigationContext)\n    {\n        ArgumentNullException.ThrowIfNull(navigationContext);\n\n        var candidateTargetContract = UriParsingHelper.EnsureAbsolute(navigationContext.Uri).AbsolutePath;\n        candidateTargetContract = candidateTargetContract.TrimStart('/');\n        return Uri.UnescapeDataString(candidateTargetContract);\n    }","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/Navigation/RegionNavigationContentLoader.cs#L59-L95","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Inspect the InnerException — it contains the actual construction failure","Fix the view's constructor so it does not throw (register all injected dependencies in the container)","Verify the view type still exists and matches the registered navigation contract name","Test resolving the view directly from the container to reproduce the construction error"],"exampleFix":"// before\npublic partial class MyPage\n{\n    public MyPage(MyService svc) // svc not registered -> construction throws\n    { InitializeComponent(); }\n}\n// after\n// in App:\ncontainerRegistry.RegisterSingleton<MyService>();\n// and/or use parameterless ctor + ViewModel injection\npublic partial class MyPage() { InitializeComponent(); }","handlingStrategy":"try-catch","validationCode":"var viewType = locatorRegistry.GetTypeForNavigation(contract);\nif (viewType is null) throw new InvalidOperationException($\"Unknown navigation target {contract}\");","typeGuard":null,"tryCatchPattern":"try { regionManager.RequestNavigate(contract); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Cannot create navigation target\"))\n{ logger.Error(ex.InnerException, \"Navigation target '{0}' failed to construct\", contract); }","preventionTips":["Register every navigable view with containerRegistry.RegisterForNavigation","Keep view constructors throw-free; use ViewModel injection","Rename contracts consistently when refactoring view classes","Inspect InnerException first when this error appears"],"tags":["regions","navigation","dependency-injection","view-creation"],"backgroundTag":"missing-dependency","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}