2

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.

pdschuller
  • 584
  • 6
  • 26

2 Answers2

0

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.

Peter Wessberg
  • 879
  • 6
  • 13
  • I removed the RegisterRoute and I still get the same results. It works on the Android device and not in the Windows version of the app. I deleted the bin and obj directories for good measure. Thanks though. – pdschuller Jul 27 '23 at 20:19
  • Ok, it is still valid so keep it. Try this: `Routing.RegisterRoute(HomeMenu/But2Sub, typeof(But2Sub));` and then `await Shell.Current.GoToAsync("//HomeMenu/But2Sub");` – Peter Wessberg Jul 27 '23 at 21:06
  • 1
    I agree with Peter's comment. What you did "should" have worked, but I suspect a bug was introduced when trying to fix a different navigation bug. This workaround, telling Shell that `But2Sub` is "under" `HomeMenu`, should correctly remove `But2Sub` from the nav stack, when navigating back to `//HomeMenu`. If this doesn't fix it, please make a public github repo with just enough info to reproduce the problem, and create a new issue at `github maui issues`. (Even if it does fix it, you are welcome to make a new issue and repo; I think it is genuinely a bug.) – ToolmakerSteve Jul 27 '23 at 21:44
  • @PeterWessberg your suggestion did not work. There are missing quotes and when the code executes the But2Sub page displays, Which makes sense b c the RegisteredRoute has typeof(But2Sub). I want the HomeMenu page to display. See what you think of the solution I came up with below. – pdschuller Jul 27 '23 at 22:17
0

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.

pdschuller
  • 584
  • 6
  • 26