PrismLibrary/Prism · error · ViewCreationException

ViewCreationException(name, _registryType, ex)

Error message

ViewCreationException(name, _registryType, ex)

What it means

CreateView wraps any exception thrown while resolving or configuring a view (other than ViewModelCreationException, which is rethrown as-is) into a ViewCreationException carrying the view name, the registry type, and the inner exception. This gives a uniform failure type for view creation errors.

Solutions

  1. Inspect the InnerException of ViewCreationException to find the real cause.
  2. Register all dependencies of the view's constructor in the DI container.
  3. Ensure the type registered under the view name derives from the registry's base view type (e.g. Page/ContentControl).
  4. Test view creation at startup (resolve it eagerly) to surface constructor errors early.

Example fix

// before — dependency missing, constructor throws
public DetailsView(IMissingService s) { ... }
// after — register the dependency
class App { RegisterTypes(...) { containerRegistry.RegisterSingleton<IMissingService, MissingService>(); containerRegistry.RegisterForNavigation<DetailsView>(); } }
Defensive patterns

Strategy: try-catch

Validate before calling

// Verify dependencies before creating the view
foreach (var p in typeof(DetailsView).GetConstructors().First().GetParameters())
    if (container.Resolve(p.ParameterType) is null)
        throw new InvalidOperationException($"Unregistered dependency {p.ParameterType.Name}");

Try / catch

try { return registry.CreateView(container, name); }
catch (ViewCreationException ex) { logger.LogError(ex.InnerException, "View '{Name}' creation failed", name); throw; }

Prevention

When it happens

Trigger: Any failure during CreateView after registration lookup succeeds: container.Resolve throwing (missing registration for the view type), SetNavigationNameProperty/SetContainerProvider/ConfigureView failing, or the view's constructor throwing — always rethrown as ViewCreationException(name, _registryType, ex).

Common situations: View's constructor throws (e.g. uninitialized service, XAML load error); DI container missing a dependency of the view; resolved object cannot be cast to TBaseView (wrong type registered under the name); platform misconfiguration.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/b7e3a1fc8ef6c48e. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Mvvm/ViewRegistryBase{TBaseView}.cs:78

            if (registration.ViewModel is not null)
                SetViewModelProperty(view, registration.ViewModel);

            Autowire(view);

            return view;
        }
        catch (KeyNotFoundException)
        {
            throw;
        }
        catch (ViewModelCreationException)
        {
            throw;
        }
        catch (Exception? ex)
        {
            throw new ViewCreationException(name, _registryType, ex);
        }
    }

    private IEnumerable<Type> GetCandidates(Type viewModelType)
    {
        var names = new List<string>
        {
            Regex.Replace(viewModelType.Name, @"ViewModel$", string.Empty),
            Regex.Replace(viewModelType.Name, @"Model$", string.Empty),
        };

        if (_registryType == ViewType.Page)
            names.Add(Regex.Replace(viewModelType.Name, @"ViewModel$", "Page"));
        else if (_registryType == ViewType.Region)
            names.Add(Regex.Replace(viewModelType.Name, @"ViewModel$", "Region"));
        else if (_registryType == ViewType.Dialog)
            names.Add(Regex.Replace(viewModelType.Name, @"ViewModel$", "Dialog"));

View on GitHub (pinned to 358118cd64)