Search code examples
navigation.net-maui.shell

Why does .NET MAUI Shell back navigation fail without raising an error


I am using shell navigation with a tab bar and four tabs. Each tab contains a content page. Some of these content pages navigate to other (e.g detail) pages and some of these go down a further level. Navigation works fine with the back button retracing its way back up the stack. On one page I have added a fourth level and it works fine. On another tab, I have added a fourth level but its back button consistently does nothing.

I modified the navigation service method to the following to aid debugging.

private Task NavigateTo(string route)
{
    var shell = Shell.Current;
    var count = shell.Navigation.NavigationStack.Count;

    foreach (Page page in shell.Navigation.NavigationStack)
    {
        if (page is not null)
        {
            Console.WriteLine(page.Title);
        }
    }

    Console.WriteLine($"Navigating to page: {route}");

    return Shell.Current.GoToAsync(route);
}

When using the back button on the fourth level the route is "..", the count is 3, and the two previous pages are listed in the console output - so a navigation stack exists.

Why does the ".." request do nothing with no error/exception being raised?


Solution

  • Solution

    Running on IOS, instead of MacCatalyst, a "System.ArgumentException: Ambiguous routes matched for" exception was raised. With further research and experimentation, the issue was duplicate/ambiguous routes on the app shell pages.

    In the AppShell.xaml I had the following as part of the tab bar definition:

            <Tab Title="Journals">
                <Tab.Icon>
                    <FontImageSource 
                        FontFamily="MaterialIcons" 
                        Glyph="{x:Static constants:MaterialIcon.Ledger}" 
                        Size="Medium"/>
                </Tab.Icon>
                <ShellContent Route="JournalsView" ContentTemplate="{DataTemplate views:JournalsView}" />
            </Tab>
    

    Then in the AppShell.xaml.cs I was also registering "JournalsView" as follows:

    Routing.RegisterRoute(nameof(JournalsView), typeof(JournalsView));
    

    Commenting out the latter, and registration of all the other routes defined in the tab bar, fixed the back navigation - even though these pages were not directly involved where the error was occurring.