From 37589f0f41fa67c03340f0e2e5ec439e43d9ba2d Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 14 Jan 2022 02:15:48 -0600 Subject: [PATCH] Route Shell Modal Navigation through ShellSection (#15053) --- .../ShellModalTests.cs | 28 +++++++++++++++++++ Xamarin.Forms.Core/Shell/Shell.cs | 15 ++++++++++ 2 files changed, 43 insertions(+) diff --git a/Xamarin.Forms.Core.UnitTests/ShellModalTests.cs b/Xamarin.Forms.Core.UnitTests/ShellModalTests.cs index 415fff80ea7..85af9de08e6 100644 --- a/Xamarin.Forms.Core.UnitTests/ShellModalTests.cs +++ b/Xamarin.Forms.Core.UnitTests/ShellModalTests.cs @@ -431,6 +431,34 @@ public async Task CanCancelPushModalAsync() Assert.AreEqual(0, shell.Navigation.ModalStack.Count); } + [Test] + public async Task PopModalFromShellNavigationProxy() + { + Routing.RegisterRoute("ModalTestPage", typeof(ModalTestPage)); + Shell shell = new Shell(); + shell.Items.Add(CreateShellItem(shellItemRoute: "NewRoute")); + + await shell.GoToAsync("ModalTestPage"); + await shell.Navigation.PopModalAsync(); + + Assert.AreEqual("//NewRoute", shell.CurrentState.Location.ToString()); + } + + [Test] + public async Task PushModalFromShellNavigationProxy() + { + ModalTestPage modalTestPage = new ModalTestPage(); + Routing.SetRoute(modalTestPage, "ModalTestPage"); + + Routing.RegisterRoute("ModalTestPage", typeof(ModalTestPage)); + Shell shell = new Shell(); + shell.Items.Add(CreateShellItem(shellItemRoute: "NewRoute")); + await shell.Navigation.PushModalAsync(modalTestPage); + + Assert.AreEqual("//NewRoute/ModalTestPage", shell.CurrentState.Location.ToString()); + } + + [QueryProperty("SomeQueryParameter", "SomeQueryParameter")] public class ModalTestPageBase : ShellLifeCycleTests.LifeCyclePage { diff --git a/Xamarin.Forms.Core/Shell/Shell.cs b/Xamarin.Forms.Core/Shell/Shell.cs index 2e8671db5e8..909cada11a3 100644 --- a/Xamarin.Forms.Core/Shell/Shell.cs +++ b/Xamarin.Forms.Core/Shell/Shell.cs @@ -1308,6 +1308,13 @@ class NavigationImpl : NavigationProxy protected override async Task OnPopModal(bool animated) { + if (!_shell.NavigationManager.AccumulateNavigatedEvents) + { + var page = _shell.CurrentPage; + await _shell.GoToAsync("..", animated); + return page; + } + if (ModalStack.Count > 0) ModalStack[ModalStack.Count - 1].SendDisappearing(); @@ -1327,6 +1334,14 @@ protected override async Task OnPopModal(bool animated) protected override async Task OnPushModal(Page modal, bool animated) { + if (!_shell.NavigationManager.AccumulateNavigatedEvents) + { + // This will route the modal push through the shell section which is setup + // to update the shell state after a modal push + await _shell.CurrentItem.CurrentItem.Navigation.PushModalAsync(modal, animated); + return; + } + if (ModalStack.Count == 0) _shell.CurrentItem.SendDisappearing();