{"record":{"id":"9191ec2f72e82d90","repo":"dotnet/maui","slug":"element-must-be-a-page","errorCode":null,"errorMessage":"Element must be a Page","messagePattern":"Element must be a Page","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Windows/NavigationPageRenderer.cs","lineNumber":165,"sourceCode":"\n\t\t\tchild.Measure(constraint);\n\t\t\tvar result = new Size(Math.Ceiling(child.DesiredSize.Width), Math.Ceiling(child.DesiredSize.Height));\n\n\t\t\tchild.Width = oldWidth;\n\t\t\tchild.Height = oldHeight;\n\n\t\t\treturn new SizeRequest(result);\n\t\t}\n\n\t\tUIElement IVisualElementRenderer.GetNativeElement()\n\t\t{\n\t\t\treturn null;\n\t\t}\n\n\t\tpublic void SetElement(VisualElement element)\n\t\t{\n\t\t\tif (element != null && !(element is NavigationPage))\n\t\t\t\tthrow new ArgumentException(\"Element must be a Page\", nameof(element));\n\n\t\t\tNavigationPage oldElement = Element;\n\t\t\tElement = (NavigationPage)element;\n\n\t\t\tif (Element != null && Element.CurrentPage is null)\n\t\t\t\tthrow new InvalidOperationException(\n\t\t\t\t\t\"NavigationPage must have a root Page before being used. Either call PushAsync with a valid Page, or pass a Page to the constructor before usage.\");\n\n\t\t\tif (oldElement is INavigationPageController navigationPageController)\n\t\t\t{\n\t\t\t\tnavigationPageController.PushRequested -= OnPushRequested;\n\t\t\t\tnavigationPageController.PopRequested -= OnPopRequested;\n\t\t\t\tnavigationPageController.PopToRootRequested -= OnPopToRootRequested;\n\t\t\t\toldElement.InternalChildren.CollectionChanged -= OnChildrenChanged;\n\t\t\t\toldElement.PropertyChanged -= OnElementPropertyChanged;\n\t\t\t}\n\n\t\t\tif (element != null)","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Windows/NavigationPageRenderer.cs#L147-L183","documentation":"Thrown as ArgumentException by NavigationPageRenderer.SetElement when the element is non-null and not a NavigationPage. The renderer is specifically for NavigationPage instances; passing any other VisualElement (ContentPage, TabbedPage, etc.) is a type mismatch.","triggerScenarios":"Calling SetElement with a non-NavigationPage VisualElement on a NavigationPageRenderer instance. This can happen if the renderer registration is misconfigured or if SetElement is called manually with the wrong element type.","commonSituations":"Renderer registration mismatch (wrong ExportRenderer attribute), manually creating a NavigationPageRenderer and passing a ContentPage, or platform-specific code that incorrectly assigns element types.","solutions":["Pass only a NavigationPage instance to NavigationPageRenderer.SetElement.","Check renderer registrations (ExportRenderer attributes) to ensure NavigationPage maps to NavigationPageRenderer.","Use the correct renderer type for the element (e.g., PageRenderer for ContentPage)."],"exampleFix":"// before\nvar renderer = new NavigationPageRenderer();\nrenderer.SetElement(contentPage); // wrong type\n\n// after\nvar renderer = new NavigationPageRenderer();\nrenderer.SetElement(new NavigationPage(contentPage)); // correct","handlingStrategy":"type-guard","validationCode":"if (element is NavigationPage || element == null)\n    renderer.SetElement(element);","typeGuard":"static bool IsValidForNavigationPageRenderer(VisualElement element) => element is NavigationPage || element == null;","tryCatchPattern":null,"preventionTips":["Match each element type with its correct renderer via ExportRenderer attributes.","Never manually pass a non-NavigationPage to a NavigationPageRenderer."],"tags":["wpf","maui-compatibility","navigationpage","renderer","type-validation"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}