Maui shell navigation, trying to get back button on second page

Phunction 256 Reputation points
2024-01-27T20:15:49.4966667+00:00

Hi, I new to Maui, I was using Xamarain iOS and Android before but now that MS is dropping support for those I have to change over.

I am trying to figure out this shell navigation. I have a main page with a button that will open a second page.

I would like the second page to be able to go back to the first page (iOS usually has the back arrow at the top) but it is not working. I think it may be with how the pages are defined in appshell.xml

<Shell
    x:Class="Test.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Test"
    Shell.FlyoutBehavior="Disabled">

    <ShellContent
        Title="Home"
        ContentTemplate="{DataTemplate local:MainPage}"
        Route="MainPage" />

    <ShellContent Route="secondTest" ContentTemplate="{DataTemplate local:SecondPage}" />

</Shell>

I open the second page like this:

await Shell.Current.GoToAsync("//secondTest");

When I try

await Shell.Current.GoToAsync("secondTest");

I get the error "Relative routing to shell elements is currently not supported." , but from what I can find , using // sets it as root so no back button? How can I get it to work? I am targeting .net7, do I need to use .net8? I don't want to use a tabbed page as the main page as I am using my own navigation bar for specific reasons, such as the page I go to will in turn have its own navigation bar.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,501 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Phunction 256 Reputation points
    2024-01-27T20:36:48.6133333+00:00

    I am not sure if it can be done in appshell.xaml, but I did find if I don't register the second page route there and instead register in the class, then it will work fine.


  2. Maarten Moonen 0 Reputation points
    2024-03-23T10:27:44.27+00:00

    Did you register your route in AppShell.xaml.cs?

    public AppShell()
    {
         InitializeComponent();
         Routing.RegisterRoute(nameof(
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.