AView GetCurrentRootView() { return(WindowMauiContext ?.GetNavigationRootManager() ?.RootView ?? throw new InvalidOperationException("Current Root View cannot be null")); }
partial void OnPageAttachedHandler() { WindowMauiContext.GetPlatformWindow().SetBackButtonPressedHandler(OnBackButtonPressed); }
void SetCurrent(Page newPage, Page previousPage, bool popping, Action?completedCallback = null) { try { if (popping) { RemovePage(previousPage); } else if (newPage.BackgroundColor.IsDefault() && newPage.Background.IsEmpty) { RemovePage(previousPage); } if (popping) { previousPage .FindMauiContext() ?.GetNavigationRootManager() ?.Disconnect(); previousPage.Handler = null; // Un-parent the page; otherwise the Resources Changed Listeners won't be unhooked and the // page will leak previousPage.Parent = null; } if (Container == null || newPage == null) { return; } // pushing modal if (!popping) { var modalContext = WindowMauiContext .MakeScoped(registerNewNavigationRoot: true); newPage.Toolbar ??= new Toolbar(newPage); _ = newPage.Toolbar.ToPlatform(modalContext); var windowManager = modalContext.GetNavigationRootManager(); windowManager.Connect(newPage.ToPlatform(modalContext)); Container.AddPage(windowManager.RootView); previousPage .FindMauiContext() ?.GetNavigationRootManager() ?.UpdateAppTitleBar(false); } // popping modal else { var windowManager = newPage.FindMauiContext()?.GetNavigationRootManager() ?? throw new InvalidOperationException("Previous Page Has Lost its MauiContext"); Container.AddPage(windowManager.RootView); windowManager.UpdateAppTitleBar(true); } completedCallback?.Invoke(); } catch (Exception error) when(error.HResult == -2147417842) { //This exception prevents the Main Page from being changed in a child //window or a different thread, except on the Main thread. //HEX 0x8001010E throw new InvalidOperationException( "Changing the current page is only allowed if it's being called from the same UI thread." + "Please ensure that the new page is in the same UI thread as the current page.", error); } }