diff --git a/src/Controls/src/Core/Window/Window.cs b/src/Controls/src/Core/Window/Window.cs index 392fa4be5507..be74cc503588 100644 --- a/src/Controls/src/Core/Window/Window.cs +++ b/src/Controls/src/Core/Window/Window.cs @@ -679,6 +679,31 @@ public NavigationImpl(Window owner) _owner = owner; } + protected override void OnInsertPageBefore(Page page, Page before) + { + throw new InvalidOperationException("InsertPageBefore is not supported, please use a NavigationPage."); + } + + protected override Task OnPushAsync(Page page, bool animated) + { + throw new InvalidOperationException("PushAsync is not supported, please use a NavigationPage."); + } + + protected override Task OnPopAsync(bool animated) + { + throw new InvalidOperationException("PopAsync is not supported, please use a NavigationPage."); + } + + protected override Task OnPopToRootAsync(bool animated) + { + throw new InvalidOperationException("PopToRootAsync is not supported, please use a NavigationPage."); + } + + protected override void OnRemovePage(Page page) + { + throw new InvalidOperationException("RemovePage is not supported, please use a NavigationPage."); + } + protected override IReadOnlyList GetModalStack() { return _owner.ModalNavigationManager.ModalStack; diff --git a/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs b/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs index 83d528bbfc23..d5b35cba4a53 100644 --- a/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs +++ b/src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs @@ -839,6 +839,29 @@ public async Task PopModalWithEmptyStackThrows() Assert.ThrowsAsync(() => window.Navigation.PopModalAsync()); } + [Fact] + public async Task InvalidOperationExceptionIsThrownWhenNavigatingOutsideNavigationPage() + { + var window = new TestWindow(new ContentPage()); + var contentPage1 = new ContentPage(); + var contentPage2 = new ContentPage(); + + Assert.ThrowsAsync(() => window.Navigation.PushAsync(contentPage1)); + Assert.ThrowsAsync(() => window.Navigation.PopAsync()); + Assert.ThrowsAsync(() => window.Navigation.PopToRootAsync()); + Assert.Throws(() => window.Navigation.InsertPageBefore(contentPage1, contentPage2)); + Assert.Throws(() => window.Navigation.RemovePage(contentPage1)); + } + + [Fact] + public async Task RemoveWrappingIntoNavigationPage() + { + var window = new TestWindow(new ContentPage()); + var contentPage1 = new ContentPage(); + var navigationPage = new TestNavigationPage(true, contentPage1); + Assert.ThrowsAsync(() => window.Navigation.PushAsync(contentPage1)); + } + [Fact] public async Task TabBarSetsOnFlyoutPageInsideModalPage() {