Search code examples
navigationmaui.net-maui.shell

How to register a non-shell-visible navigation route in Maui Shell?


I have a Maui app, with a page that should not be visible in the Shell items.

I try to register it, according to documentation and several articles like this:

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();
        Routing.RegisterRoute(nameof(ItemPage), typeof(ItemPage));
    }
}

In my event handler in the ViewModel that implements the command called:

        async void OnItemTapped(ItemViewModel itemVM)
        {
            string route =
                $"//{nameof(ItemPage)}?{nameof(ItemPage.Id)}={itemVM.Id}";
            await Shell.Current.GoToAsync(route);
        }

In debugging, I can verify that the contents of variable route are as expected. On excuting GotoAsync() I get an exception: "System.ArgumentException: 'unable to figure out route for: //ItemPage?Id=1-0 (Parameter 'uri')'".

If I register the same route from xaml, route resolution in GotoAsync works:

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    ...
    <TabBar>
        <ShellContent
            Title="ItemPage"
            ContentTemplate="{DataTemplate local:ItemPage}"
            Route="ItemPage" />
    </TabBar>
</Shell>

I tried

  • converting all text to lowercase at registration and navigation
  • prefixing the route with "//" when registering

Questions: 1.) Am I doing something wrong when I try to programmatically register a route? Routing.RegisterRoute(nameof(ItemPage), typeof(ItemPage));

2.) If this is buggy in Maui what is an alternative way of registering a page route without showing it on the shell menu?

Environment is:

  • Visual Studio 2022, v17.4.3
  • Maui v7

Solution

    • Remove // from start of string route = $"//...

    I think the programmatic one, because it is not part of Shell's hierarchy, violates Invalid Routes:

    "Global routes currently can't be the only page on the navigation stack.".

    The // clears nav stack, attempts to push specified page.
    Works when part of Shell hierarchy, not when an independent page.