{"record":{"id":"dac82188ca816c14","repo":"lepoco/wpfui","slug":"pagetype-of-the-typeof-inavigationviewitem-mus","errorCode":null,"errorMessage":"PageType of the ${typeof(INavigationViewItem)} must be derived from {typeof(FrameworkElement)}. {pageType} is not.","messagePattern":"PageType of the (.+?) must be derived from (.+?)\\. (.+?) is not\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/NavigationView/NavigationViewActivator.cs","lineNumber":29,"sourceCode":"// ReSharper disable once CheckNamespace\nnamespace Wpf.Ui.Controls;\n\n/// <summary>\n/// Internal activator for creating content instances of the navigation view items.\n/// </summary>\ninternal static class NavigationViewActivator\n{\n    /// <summary>\n    /// Creates new instance of type derived from <see cref=\"FrameworkElement\"/>.\n    /// </summary>\n    /// <param name=\"pageType\"><see cref=\"FrameworkElement\"/> to instantiate.</param>\n    /// <param name=\"dataContext\">Additional context to set.</param>\n    /// <returns>Instance of the <see cref=\"FrameworkElement\"/> object or <see langword=\"null\"/>.</returns>\n    public static FrameworkElement? CreateInstance(Type pageType, object? dataContext = null)\n    {\n        if (!typeof(FrameworkElement).IsAssignableFrom(pageType))\n        {\n            throw new InvalidCastException(\n                $\"PageType of the ${typeof(INavigationViewItem)} must be derived from {typeof(FrameworkElement)}. {pageType} is not.\"\n            );\n        }\n\n        if (DesignerHelper.IsInDesignMode)\n        {\n            return new Page\n            {\n                Content = new TextBlock\n                {\n                    Text =\n                        \"Pages are not rendered while using the Designer. Edit the page template directly.\",\n                },\n            };\n        }\n\n        FrameworkElement? instance;\n","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/NavigationView/NavigationViewActivator.cs#L11-L47","documentation":"NavigationViewActivator.CreateInstance rejects any page type that is not assignable to FrameworkElement by throwing InvalidCastException. WPF UI navigation only renders FrameworkElement-derived content (Page, UserControl, Window-embedded controls), so a type that fails this check can never be hosted. This is a hard contract check performed before any constructor logic runs.","triggerScenarios":"An INavigationViewItem.TargetPageType (or a Navigate call's generic type argument) is set to a class/struct that does not inherit FrameworkElement - e.g. a plain ViewModel, a record, a Window subclass, or an interface.","commonSituations":"Accidentally binding TargetPageType to a ViewModel instead of its View; navigating to a data model type; refactoring a page into a non-FrameworkElement base class; copy/paste of a type name from XAML that resolves to the wrong symbol.","solutions":["Point TargetPageType at a UserControl or Page that derives from FrameworkElement.","If you intend to navigate by ViewModel, introduce a View-ViewModel mapping in an INavigationViewPageProvider and resolve the View type.","Double-check the XAML x:Name/Type resolution to ensure the symbol is the view, not a sibling model with the same name."],"exampleFix":"// before\n<ui:NavigationViewItem TargetPageType=\"{x:Type vm:DashboardViewModel}\" />\n\n// after\n<ui:NavigationViewItem TargetPageType=\"{x:Type views:DashboardPage}\" />","handlingStrategy":"validation","validationCode":"if (!typeof(FrameworkElement).IsAssignableFrom(viewItem.TargetPageType))\n{\n    throw new ArgumentException(\n        $\"TargetPageType {viewItem.TargetPageType} must derive from FrameworkElement.\");\n}","typeGuard":"static bool IsValidPageType(Type? t) => t is not null && typeof(FrameworkElement).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface;","tryCatchPattern":"try { navView.Navigate(pageType); }\ncatch (InvalidCastException ex) when (ex.Message.Contains(\"must be derived from\"))\n{\n    _logger.LogError(\"TargetPageType {Type} is not a FrameworkElement\", pageType);\n}","preventionTips":["Always point TargetPageType at UserControl/Page types via x:Type.","Review navigation item bindings when refactoring ViewModels to ensure they still reference views.","Add a startup check that validates all declared TargetPageType values are FrameworkElement-derived."],"tags":["navigation","type-validation","activator","wpf"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}