PrismLibrary/Prism · error · NavigationException

NavigationException.ErrorCreatingViewModel

NavigationException.ErrorCreatingViewModel

Error message

NavigationException.ErrorCreatingViewModel

What it means

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.

Solutions

  1. Inspect InnerException (vmce) to find the actual unresolvable type, then register it with the container
  2. Register the view model with RegisterForNavigation so Prism can construct it in the navigation scope
  3. Fix or guard the throwing constructor of the injected dependency

Example fix

// before
class SettingsViewModel(ISettingsService svc) // ISettingsService never registered
// after
containerRegistry.RegisterSingleton<ISettingsService, SettingsService>();
containerRegistry.RegisterForNavigation<SettingsPage, SettingsViewModel>();
Defensive patterns

Strategy: try-catch

Validate before calling

// proactively verify VM dependencies are registered
if (!containerRegistry.IsRegistered<ISettingsService>()) throw new InvalidOperationException("Register ISettingsService");

Try / catch

try { await _navigationService.NavigateAsync(segment); }
catch (NavigationException ne) when (ne.Message == NavigationException.ErrorCreatingViewModel) { Debug.WriteLine(ne.InnerException); }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

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/765364867ffb3893. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/PageNavigationService.cs:951

            var scope = _container.CreateScope();
            var page = (Page)Registry.CreateView(scope, segmentName);

            if (page is null)
                throw new NullReferenceException($"The resolved type for {segmentName} was null. You may be attempting to navigate to a Non-Page type");

            return page;
        }
        catch(NavigationException)
        {
            throw;
        }
        catch(KeyNotFoundException knfe)
        {
            throw new NavigationException(NavigationException.NoPageIsRegistered, segmentName, knfe);
        }
        catch(ViewModelCreationException vmce)
        {
            throw new NavigationException(NavigationException.ErrorCreatingViewModel, segmentName, _pageAccessor.Page, vmce);
        }
        //catch(ViewCreationException viewCreationException)
        //{
        //    if(!string.IsNullOrEmpty(viewCreationException.InnerException?.Message) && viewCreationException.InnerException.Message.Contains("Maui"))
        //        throw new NavigationException(NavigationException.)
        //}
        catch (Exception ex)
        {
            var inner = ex.InnerException;
            while(inner is not null)
            {
                if (inner.Message.Contains("thread with a dispatcher"))
                    throw new NavigationException(NavigationException.UnsupportedMauiCreation, segmentName, _pageAccessor.Page, ex);
                inner = inner.InnerException;
            }
            throw new NavigationException(NavigationException.ErrorCreatingPage, segmentName, ex);
        }
    }

View on GitHub (pinned to 358118cd64)