MAUI AppShell route works on Android but not on Windows

118 Views Asked by At

I made a demo maui app and I have found that this line of code:

AppShell.Current.GoToAsync("//HomeMenu");

works fine when the app is run to an Android local device but does not work when the app is running as a Windows app.

I have registered the routes in AppShell.cs like this:

Routing.RegisterRoute(nameof(HomeMenu), typeof(HomeMenu));
Routing.RegisterRoute(nameof(But2Page), typeof(But2Page));
Routing.RegisterRoute(nameof(But2Sub), typeof(But2Sub));
Routing.RegisterRoute(nameof(But2SubSub), typeof(But2SubSub));

Here is a representation of the ContentPages I am displaying for this use case:

Home Menu
    - Button2 Page
        □ Button 2 Sub Page
            - Button 2 Sub Sub Page

Here are the commands that the code executes to walk down that hierarchy

AppShell.Current.GoToAsync("But2Page");
AppShell.Current.GoToAsync("But2Sub");
AppShell.Current.GoToAsync("But2SubSub");

That all works fine on both platforms. On the But2SubSub page the code does the following:

AppShell.Current.GoToAsync("//HomeMenu");

On Android the app displays the Home Menu page. On Windows the value at the top of the page displays "Home Menu" but the page content remains the But2SubSub content:

enter image description here

NOTE: I have not declared any routes at all in the AppShell.xaml file. It simply has this:

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

How can I make the last GoToAsync method work correctly in the Windows app?

Thanks.

2

There are 2 best solutions below

4
Peter Wessberg On

You cannot have the same route defined twice. You have

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

And also

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

So I suggest you remove the RegisterRoute and it will work.

0
pdschuller On

To be clear, the app is displaying the But2SubSub page and I want the code (a button click) to make the app display the HomeMenu page. The following edits work.

Routing.RegisterRoute("SomeUniqueName", typeof(HomeMenu));

Then in the click handler:

AppShell.Current.GoToAsync("SomeUniqueName");

I have a lot of navigation on a lot of button clicks in the app, because that is what I am testing out, and that all still works with the changes above.