{"record":{"id":"765364867ffb3893","repo":"PrismLibrary/Prism","slug":"navigationexception-errorcreatingviewmodel","errorCode":"NavigationException.ErrorCreatingViewModel","errorMessage":"NavigationException.ErrorCreatingViewModel","messagePattern":"NavigationException\\.ErrorCreatingViewModel","errorType":"exception","errorClass":"NavigationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/PageNavigationService.cs","lineNumber":951,"sourceCode":"            var scope = _container.CreateScope();\n            var page = (Page)Registry.CreateView(scope, segmentName);\n\n            if (page is null)\n                throw new NullReferenceException($\"The resolved type for {segmentName} was null. You may be attempting to navigate to a Non-Page type\");\n\n            return page;\n        }\n        catch(NavigationException)\n        {\n            throw;\n        }\n        catch(KeyNotFoundException knfe)\n        {\n            throw new NavigationException(NavigationException.NoPageIsRegistered, segmentName, knfe);\n        }\n        catch(ViewModelCreationException vmce)\n        {\n            throw new NavigationException(NavigationException.ErrorCreatingViewModel, segmentName, _pageAccessor.Page, vmce);\n        }\n        //catch(ViewCreationException viewCreationException)\n        //{\n        //    if(!string.IsNullOrEmpty(viewCreationException.InnerException?.Message) && viewCreationException.InnerException.Message.Contains(\"Maui\"))\n        //        throw new NavigationException(NavigationException.)\n        //}\n        catch (Exception ex)\n        {\n            var inner = ex.InnerException;\n            while(inner is not null)\n            {\n                if (inner.Message.Contains(\"thread with a dispatcher\"))\n                    throw new NavigationException(NavigationException.UnsupportedMauiCreation, segmentName, _pageAccessor.Page, ex);\n                inner = inner.InnerException;\n            }\n            throw new NavigationException(NavigationException.ErrorCreatingPage, segmentName, ex);\n        }\n    }","sourceCodeStart":933,"sourceCodeEnd":969,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/PageNavigationService.cs#L933-L969","documentation":"While creating a page for a navigation segment, constructing the page's view model threw a ViewModelCreationException (typically a container resolution failure for the VM or one of its constructor dependencies). Prism re-wraps it as NavigationException.ErrorCreatingViewModel with the segment name and current page attached.","triggerScenarios":"The registered view model, or any dependency in its constructor, cannot be resolved by the DI container (unregistered service, missing registration, exception in a dependency's constructor).","commonSituations":"Forgetting to register a service the VM injects; a dependency throwing in its own constructor (config file missing, network init); switching lifetimes so a required dependency isn't available in the navigation scope.","solutions":["Inspect InnerException (vmce) to find the actual unresolvable type, then register it with the container","Register the view model with RegisterForNavigation so Prism can construct it in the navigation scope","Fix or guard the throwing constructor of the injected dependency"],"exampleFix":"// before\nclass SettingsViewModel(ISettingsService svc) // ISettingsService never registered\n// after\ncontainerRegistry.RegisterSingleton<ISettingsService, SettingsService>();\ncontainerRegistry.RegisterForNavigation<SettingsPage, SettingsViewModel>();","handlingStrategy":"try-catch","validationCode":"// proactively verify VM dependencies are registered\nif (!containerRegistry.IsRegistered<ISettingsService>()) throw new InvalidOperationException(\"Register ISettingsService\");","typeGuard":null,"tryCatchPattern":"try { await _navigationService.NavigateAsync(segment); }\ncatch (NavigationException ne) when (ne.Message == NavigationException.ErrorCreatingViewModel) { Debug.WriteLine(ne.InnerException); }","preventionTips":["Register every service injected into view models","Use constructor validation / container scope verification at startup","Keep heavy logic out of VM constructors to reduce throw surfaces"],"tags":["prism","maui","navigation","dependency-injection","viewmodel"],"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"}