{"record":{"id":"3b0c433e267203cb","repo":"PrismLibrary/Prism","slug":"viewmodelcreationexception-wrapping-the-original-exception","errorCode":null,"errorMessage":"ViewModelCreationException wrapping the original exception for the view","messagePattern":"ViewModelCreationException wrapping the original exception for the view","errorType":"exception","errorClass":"ViewModelCreationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/PrismAppBuilder.cs","lineNumber":150,"sourceCode":"\n    internal static object DefaultViewModelLocator(object view, Type viewModelType)\n    {\n        try\n        {\n            if (view is not BindableObject bindable || bindable.BindingContext is not null)\n                return null;\n\n            var container = bindable.GetContainerProvider();\n\n            return container.Resolve(viewModelType, (typeof(IDispatcher), bindable.Dispatcher));\n        }\n        catch (ViewModelCreationException)\n        {\n            throw;\n        }\n        catch (Exception ex)\n        {\n            throw new ViewModelCreationException(view, ex);\n        }\n    }\n\n    /// <summary>\n    /// Provides a Delegate to register services with the <see cref=\"PrismAppBuilder\"/>\n    /// </summary>\n    /// <param name=\"registerTypes\">The delegate to register your services.</param>\n    /// <returns>The <see cref=\"PrismAppBuilder\"/>.</returns>\n    public PrismAppBuilder RegisterTypes(Action<IContainerRegistry> registerTypes)\n    {\n        _registrations.Add(registerTypes);\n        return this;\n    }\n\n    /// <summary>\n    /// Provides a Delegate to invoke when the App is initialized.\n    /// </summary>\n    /// <param name=\"action\">The delegate to invoke.</param>","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/PrismAppBuilder.cs#L132-L168","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the InnerException of the ViewModelCreationException to find the real cause (missing registration or constructor exception).","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).","Check that the ViewModel can be resolved with a parameterless or fully-resolvable constructor; remove or register any failing dependency.","Verify view-to-VM naming conventions or explicit VM registration if using auto-wiring."],"exampleFix":"// before: VM depends on unregistered service\npublic class OrdersViewModel(IOrderApi api) { ... }\n// after: register the dependency in App\nprotected override void RegisterTypes(IContainerRegistry registry)\n{\n    registry.RegisterSingleton<IOrderApi, OrderApi>();\n}","handlingStrategy":"try-catch","validationCode":"// Verify the VM resolves before navigating\nvar vm = container.Resolve<MyViewModel>();","typeGuard":"static bool CanCreate<TView>() => App.Current.Container.IsRegistered(typeof(TView));","tryCatchPattern":"try\n{\n    await NavigationService.NavigateAsync(\"MainPage\");\n}\ncatch (ViewModelCreationException ex)\n{\n    logger.LogError(ex.InnerException, \"VM creation failed for {View}\", ex.Message);\n}","preventionTips":["Register every ViewModel and its constructor dependencies in RegisterTypes.","Keep view/VM naming conventions consistent for auto-wiring.","Add a startup smoke test that resolves each ViewModel from the container."],"tags":["dotnet","maui","dependency-injection","viewmodel","prism"],"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"}