Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Controls/src/Core/Routing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,36 @@ internal static void ClearImplicitPageRoutes()
s_routeKeys = null;
}

internal static void RegisterImplicitPageRoute(Page page)
internal static Page RegisterImplicitPageRoute(Page page)
{
var route = GetRoute(page);
if (!IsUserDefined(route))
{
s_implicitPageRoutes.TryGetValue(route, out var previousPage);
s_implicitPageRoutes[route] = page;
s_routeKeys = null;

return previousPage;
}

return null;
}

internal static void UnregisterImplicitPageRoute(Page page, string route, Page previousPage = null)
{
if (s_implicitPageRoutes.TryGetValue(route, out var registeredPage) &&
ReferenceEquals(registeredPage, page))
{
if (previousPage is not null)
{
s_implicitPageRoutes[route] = previousPage;
}
else
{
s_implicitPageRoutes.Remove(route);
}

s_routeKeys = null;
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/Controls/src/Core/Shell/ShellNavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ internal async Task GoToAsync(
if (_shell?.CurrentItem?.CurrentItem?.PendingNavigationTask != null)
await (_shell?.CurrentItem?.CurrentItem?.PendingNavigationTask ?? Task.CompletedTask);

if (shellNavigationParameters.PagePushing != null && navigationRequest == null)
Routing.RegisterImplicitPageRoute(shellNavigationParameters.PagePushing);
bool registeredPagePushingRoute = shellNavigationParameters.PagePushing is not null && navigationRequest is null;
string pagePushingRoute = null;
Page previousPagePushingRoute = null;
if (registeredPagePushingRoute)
{
pagePushingRoute = Routing.GetRoute(shellNavigationParameters.PagePushing);
previousPagePushingRoute = Routing.RegisterImplicitPageRoute(shellNavigationParameters.PagePushing);
}

var state = shellNavigationParameters.TargetState ?? new ShellNavigationState(Routing.GetRoute(shellNavigationParameters.PagePushing), false);
bool? animate = shellNavigationParameters.Animated;
Expand Down Expand Up @@ -82,7 +88,14 @@ internal async Task GoToAsync(
accept = await navigatingArgs.DeferredTask;

if (!accept)
{
if (registeredPagePushingRoute)
{
Routing.UnregisterImplicitPageRoute(shellNavigationParameters.PagePushing, pagePushingRoute, previousPagePushingRoute);
}

return;
}
}
}

Expand Down
62 changes: 62 additions & 0 deletions src/Controls/tests/Core.UnitTests/ShellNavigatingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,68 @@ public void CancelNavigation()
Assert.Equal("//one/tabone/content", shell.CurrentState.Location.ToString());
}

[Fact]
public async Task CancelledPushRemovesOnlyPushedPageImplicitRoute()
{
var shell = new TestShell
{
Items = { CreateShellItem<FlyoutItem>() }
};
var existingPage = new ContentPage();
var cancelledPage = new ContentPage();
var existingRoute = Routing.GetRoute(existingPage);
var cancelledRoute = Routing.GetRoute(cancelledPage);

Routing.RegisterImplicitPageRoute(existingPage);
shell.Navigating += (_, args) => args.Cancel();

await shell.Navigation.PushAsync(cancelledPage);

Assert.Same(existingPage, Routing.GetOrCreateContent(existingRoute));
Assert.Null(Routing.GetOrCreateContent(cancelledRoute));
}

[Fact]
public async Task CancelledPushRestoresDisplacedImplicitRoute()
{
const string route = "IMPL_cancelledPush";
var shell = new TestShell
{
Items = { CreateShellItem<FlyoutItem>() }
};
var existingPage = new ContentPage();
var cancelledPage = new ContentPage();
Routing.SetRoute(existingPage, route);
Routing.SetRoute(cancelledPage, route);

Routing.RegisterImplicitPageRoute(existingPage);
shell.Navigating += (_, args) => args.Cancel();

await shell.Navigation.PushAsync(cancelledPage);

Assert.Same(existingPage, Routing.GetOrCreateContent(route));
}

[Fact]
public async Task CancelledPushRemovesImplicitRouteWhenPageRouteChangesDuringNavigation()
{
var shell = new TestShell
{
Items = { CreateShellItem<FlyoutItem>() }
};
var cancelledPage = new ContentPage();
var registeredRoute = Routing.GetRoute(cancelledPage);
shell.Navigating += (_, args) =>
{
Routing.SetRoute(cancelledPage, "ChangedDuringNavigating");
args.Cancel();
};

await shell.Navigation.PushAsync(cancelledPage);

Assert.Null(Routing.GetOrCreateContent(registeredRoute));
}

[Fact]
public void CancelNavigationOccurringOutsideGotoAsyncWithoutDelay()
{
Expand Down
Loading