{"record":{"id":"e3a9165484eeb0fe","repo":"lepoco/wpfui","slug":"the-pagetype-page-does-not-have-a-parameterless-e3a916","errorCode":null,"errorMessage":"The {pageType} page does not have a parameterless constructor. If you are using {typeof(INavigationViewPageProvider)} do not navigate initially and don't use Cache or Precache.","messagePattern":"The (.+?) page does not have a parameterless constructor\\. If you are using (.+?) do not navigate initially and don't use Cache or Precache\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/NavigationView/NavigationViewActivator.cs","lineNumber":87,"sourceCode":"                return instance;\n            }\n        }\n        else if (dataContext != null)\n#else\n        if (dataContext != null)\n#endif\n        {\n            instance = InvokeElementConstructor(pageType, dataContext);\n\n            if (instance != null)\n            {\n                return instance;\n            }\n        }\n\n        ConstructorInfo emptyConstructor =\n            FindParameterlessConstructor(pageType)\n            ?? throw new InvalidOperationException(\n                $\"The {pageType} page does not have a parameterless constructor. If you are using {typeof(INavigationViewPageProvider)} do not navigate initially and don't use Cache or Precache.\"\n            );\n        instance = emptyConstructor.Invoke(null) as FrameworkElement;\n        SetDataContext(instance, dataContext);\n\n        return instance;\n    }\n\n#if NET48_OR_GREATER || NETCOREAPP3_0_OR_GREATER\n    private static object? ResolveConstructorParameter(Type tParam, object? dataContext)\n    {\n        if (dataContext != null && dataContext.GetType() == tParam)\n        {\n            return dataContext;\n        }\n\n        return ControlsServices.ControlsServiceProvider?.GetService(tParam);\n    }","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/NavigationView/NavigationViewActivator.cs#L69-L105","documentation":"The final fallback in CreateInstance: after the DI branch and the dataContext branch, the activator looks for a public parameterless constructor via FindParameterlessConstructor. If none exists it throws InvalidOperationException. This fires on the non-DI code path (or when DI is not configured) for pages that only define parameterized constructors.","triggerScenarios":"A page that defines only constructors taking arguments is navigated to without a registered IServiceProvider/INavigationViewPageProvider and without a matching dataContext constructor.","commonSituations":"A page written for DI (constructor takes services) being opened in a context where DI is not wired; removing the implicit parameterless constructor after adding a custom one; design-time or unit-test hosting that lacks the full DI container.","solutions":["Add an explicit public parameterless constructor to the page.","Register an INavigationViewPageProvider / IServiceProvider so the page is resolved via DI instead of reflection.","Supply a dataContext whose type matches one of the page's constructors.","Do not set Cache/Precache or initial navigation for pages that require DI."],"exampleFix":"// before\npublic class SettingsPage : UserControl\n{\n    public SettingsPage(ISettingsService svc) { ... }\n}\n\n// after\npublic class SettingsPage : UserControl\n{\n    public SettingsPage() : this(new SettingsService()) { }\n    public SettingsPage(ISettingsService svc) { ... }\n}","handlingStrategy":"validation","validationCode":"var hasParameterless = typeof(TPage).GetConstructor(Type.EmptyTypes) is not null;\nif (!hasParameterless && ControlsServices.ControlsServiceProvider is null)\n{\n    throw new InvalidOperationException($\"{typeof(TPage)} needs a parameterless ctor or DI.\");\n}","typeGuard":"static bool HasUsableConstructor(Type t) =>\n    t.GetConstructor(Type.EmptyTypes) is not null || ControlsServices.ControlsServiceProvider is not null;","tryCatchPattern":"try { navView.Navigate<TPage>(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"parameterless constructor\"))\n{\n    _logger.LogError(ex, \"No parameterless ctor for {Page}\", typeof(TPage));\n}","preventionTips":["Add an explicit parameterless constructor to navigable pages.","Wire DI when pages require dependencies.","Disable Cache/Precache for pages that are not safely constructible."],"tags":["navigation","reflection","activator","wpf"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}