{"record":{"id":"9f695ddeb0f31520","repo":"dotnet/maui","slug":"navigationpage-must-have-a-root-page-before-being-9f695d","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/MacOS/Renderers/NavigationPageRenderer.cs","lineNumber":204,"sourceCode":"\t\t{\n\t\t\tvar removed = await PopPageAsync(page, animated);\n\t\t\tPlatform.NativeToolbarTracker.UpdateToolBar();\n\t\t\treturn removed;\n\t\t}\n\n\t\tprotected virtual async Task<bool> OnPush(Page page, bool animated)\n\t\t{\n\t\t\tvar shown = await AddPage(page, animated);\n\t\t\tPlatform.NativeToolbarTracker.UpdateToolBar();\n\t\t\treturn shown;\n\t\t}\n\n\t\tvoid Init()\n\t\t{\n\t\t\tConfigurePageRenderer();\n\n\t\t\tif (NavigationPage.CurrentPage == 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\tPlatform.NativeToolbarTracker.Navigation = NavigationPage;\n\n\t\t\tNavigationPage.PushRequested += OnPushRequested;\n\t\t\tNavigationPage.PopRequested += OnPopRequested;\n\t\t\tNavigationPage.PopToRootRequested += OnPopToRootRequested;\n\t\t\tNavigationPage.RemovePageRequested += OnRemovedPageRequested;\n\t\t\tNavigationPage.InsertPageBeforeRequested += OnInsertPageBeforeRequested;\n\t\t\tNavigationPage.Popped += OnPopped;\n\t\t\tNavigationPage.PoppedToRoot += OnPoppedToRoot;\n\n\t\t\tUpdateBarBackgroundColor();\n\t\t\tUpdateBarBackground();\n\t\t\tUpdateBarTextColor();\n\n\t\t\t_events = new EventTracker(this);\n\t\t\t_events.LoadEvents(NativeView);","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/MacOS/Renderers/NavigationPageRenderer.cs#L186-L222","documentation":"The macOS NavigationPageRenderer.Init requires NavigationPage.CurrentPage to be non-null before wiring navigation events and the toolbar. CurrentPage is null when the NavigationPage was built without a root page and nothing was pushed before rendering (the parameterless NavigationPage() ctor leaves it null). Without a current page the renderer cannot show anything, so it throws InvalidOperationException. This is the macOS counterpart of the GTK error 104.","triggerScenarios":"Constructing new NavigationPage() (no root) and then rendering it (assigning to MainPage) before any PushAsync. NavigationPage.CurrentPage == null at renderer Init time.","commonSituations":"Parameterless NavigationPage() used with intent to push later, but rendered immediately. Refactor that moved the first PushAsync after MainPage assignment. Subclass that defers root creation.","solutions":["Pass a root page to the constructor: var nav = new NavigationPage(new ContentPage()); MainPage = nav;","If using the parameterless ctor, PushAsync the root before the page is rendered (before MainPage assignment).","Ensure custom NavigationPage subclasses always set a root before becoming visible."],"exampleFix":"// before\nvar nav = new NavigationPage();\nMainPage = nav; // throws: CurrentPage null\n\n// after\nvar nav = new NavigationPage(new HomePage());\nMainPage = nav;","handlingStrategy":"validation","validationCode":"var nav = new NavigationPage();\nif (nav.CurrentPage == null)\n    await nav.PushAsync(new ContentPage());\nMainPage = nav;","typeGuard":"static bool HasRoot(NavigationPage np) => np.CurrentPage != null;","tryCatchPattern":null,"preventionTips":["Prefer the NavigationPage(Page root) constructor.","Push a root before assigning a parameterless NavigationPage to MainPage.","Test that MainPage is a NavigationPage with a non-null CurrentPage."],"tags":["macos","navigationpage","renderer","lifecycle","maui","xamarin"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}