PrismLibrary/Prism · error · ViewModelCreationException

ViewModelCreationException wrapping the original exception…

Error message

ViewModelCreationException wrapping the original exception for the view

What it means

When Prism's DefaultViewModelLocator fails to create a view model for a view, it catches the original exception and rethrows it wrapped in a ViewModelCreationException carrying a reference to the view. This preserves the original exception as InnerException while adding context about which view's VM creation failed.

Solutions

  1. Inspect the InnerException of the ViewModelCreationException to find the real cause (missing registration or constructor exception).
  2. Register the view model and all of its constructor dependencies with the container (e.g. services.AddTransient<MyViewModel>() or via Prism's Register in App.CreateWindow/registration extensions).
  3. Check that the ViewModel can be resolved with a parameterless or fully-resolvable constructor; remove or register any failing dependency.
  4. Verify view-to-VM naming conventions or explicit VM registration if using auto-wiring.

Example fix

// before: VM depends on unregistered service
public class OrdersViewModel(IOrderApi api) { ... }
// after: register the dependency in App
protected override void RegisterTypes(IContainerRegistry registry)
{
    registry.RegisterSingleton<IOrderApi, OrderApi>();
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Verify the VM resolves before navigating
var vm = container.Resolve<MyViewModel>();

Type guard

static bool CanCreate<TView>() => App.Current.Container.IsRegistered(typeof(TView));

Try / catch

try
{
    await NavigationService.NavigateAsync("MainPage");
}
catch (ViewModelCreationException ex)
{
    logger.LogError(ex.InnerException, "VM creation failed for {View}", ex.Message);
}

Prevention

When it happens

Trigger: Navigating to a page whose view model cannot be constructed: the VM type is not registered in the container, its constructor dependencies are unregistered, or the VM constructor throws.

Common situations: Forgetting to register a service injected into the ViewModel; a constructor throwing (bad config, null settings); typos in auto-wiring conventions like naming the VM differently from the View; DI scope/lifetime mistakes after upgrading Prism.Maui versions.

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/3b0c433e267203cb. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/PrismAppBuilder.cs:150

    internal static object DefaultViewModelLocator(object view, Type viewModelType)
    {
        try
        {
            if (view is not BindableObject bindable || bindable.BindingContext is not null)
                return null;

            var container = bindable.GetContainerProvider();

            return container.Resolve(viewModelType, (typeof(IDispatcher), bindable.Dispatcher));
        }
        catch (ViewModelCreationException)
        {
            throw;
        }
        catch (Exception ex)
        {
            throw new ViewModelCreationException(view, ex);
        }
    }

    /// <summary>
    /// Provides a Delegate to register services with the <see cref="PrismAppBuilder"/>
    /// </summary>
    /// <param name="registerTypes">The delegate to register your services.</param>
    /// <returns>The <see cref="PrismAppBuilder"/>.</returns>
    public PrismAppBuilder RegisterTypes(Action<IContainerRegistry> registerTypes)
    {
        _registrations.Add(registerTypes);
        return this;
    }

    /// <summary>
    /// Provides a Delegate to invoke when the App is initialized.
    /// </summary>
    /// <param name="action">The delegate to invoke.</param>

View on GitHub (pinned to 358118cd64)