2

On Page#3, by pressing build in back button, its going back to page 2. so far so good

But, On Page#4, by pressing build in back button, it doesnt go back to Page#3. Why not?

here is log: according to logs, issue is in Page#3

[0:] Shell: Failed to Navigate Back: 
System.ArgumentException: 
Ambiguous routes matched for: //D_FAULT_FlyoutItem7/IMPL_MyWalletPage/MyWalletPage/Page3 matches found: 
//D_FAULT_FlyoutItem7/IMPL_MyWalletPage/MyWalletPage/Page3,//D_FAULT_FlyoutItem7/IMPL_MyWalletPage/MyWalletPage/Page3
    Parameter name: uri
  at Xamarin.Forms.ShellUriHandler.GetNavigationRequest (Xamarin.Forms.Shell shell, System.Uri uri, System.Boolean enableRelativeShellRoutes, System.Boolean throwNavigationErrorAsException, Xamarin.Forms.ShellNavigationParameters shellNavigationParameters) [0x000aa] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellUriHandler.cs:207 
  at Xamarin.Forms.ShellNavigationManager.GoToAsync (Xamarin.Forms.ShellNavigationParameters shellNavigationParameters) [0x000b8] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellNavigationManager.cs:44 
  at Xamarin.Forms.ShellSection+NavigationImpl.OnPopAsync (System.Boolean animated) [0x000e9] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellSection.cs:1070 
  at Xamarin.Forms.Platform.Android.ShellToolbarTracker.OnNavigateBack () [0x0002a] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ShellToolbarTracker.cs:207 

Page#1: I added //becuase i want ShellApp build-in menu in Page#2, and not a back button

 async Task OnKeypadSubmitTapCommand()
{
     var route = $"//{ nameof(Page2)}";
     await Shell.Current.GoToAsync(route);
 }

Page#2: Shell.Current.CurrentState.Location here is \\Page2

 private async void Button_Clicked(object sender, EventArgs e)
    {
        var route = $"{ nameof(Page3)}";
        await Shell.Current.GoToAsync(route);
    }

Page#3: Shell.Current.CurrentState.Location here is \\Page2

 private async void Button_Clicked(object sender, EventArgs e)
    {
        var route = $"{ nameof(Page4)}";
        await Shell.Current.GoToAsync(route);
    }

Page#4: Shell.Current.CurrentState.Location here is \\Page2\Page3

 private async void Button_Clicked(object sender, EventArgs e)
    {
        ...
    }

register routes

Routing.RegisterRoute(nameof(Page0), typeof(Page0));
Routing.RegisterRoute(nameof(Page1), typeof(Page1));
Routing.RegisterRoute(nameof(Page2), typeof(Page2));
Routing.RegisterRoute(nameof(Page3), typeof(Page3));
Routing.RegisterRoute(nameof(Page4), typeof(Page4));

FlyoutItem

<FlyoutItem Title="Splash Screen" Icon="icon_about.png" FlyoutItemIsVisible="false">
    <ShellContent Route="Page0" ContentTemplate="{DataTemplate local:Page0}" />
</FlyoutItem>
<FlyoutItem Title="HomePage" Icon="icon_about.png">
    <ShellContent Route="Page2" ContentTemplate="{DataTemplate local:Page2}" />
</FlyoutItem>

Folders

>Folder1
  >Page#1
>Folder2 
   >Page#3
   >Page#4
>Page#2

I looked on google but no solution. I know GoToAsync works by stacking pages pushing on top and popping it. in my code above, from page#2, I am starting to stack up pages so going back should work

  • *"GoToAsync works by stacking pages pushing on top and popping it"* - that was always true in the older-style navigation. BUT when using `Routing`, its (sometimes) different. The key statement in https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation is *"When a route from the Shell visual hierarchy is navigated to, a navigation stack isn't created."*. I haven't worked with Routing myself; not sure if there is a good tutorial that would show exactly what to expect when. – ToolmakerSteve May 22 '21 at 21:24

2 Answers2

3

or anyone that will face this issue, and have this error message too;

 System.ArgumentException: 'Ambiguous routes matched for: ...'

This occures when you register your routes in XAML (in the appshell file) and you also try registering your routes in code behind in C#. Only register your routes once using either XAML or C# not both.

In my case, I had Page2 register twice. i removed in c# and it worked for me

  • OMG this was the issue! I registered the pages in the XAML and the CS and this caused the weired behaviour. THANK YOU VERY MUCH for sharing your solution. – user3772108 Aug 29 '21 at 09:47
3

user15900757 is right but the answer is not clear...

I just spent over a day tracking this down and if I had hair left, it would all be pulled out now.

The page that was experiencing the issue was two navigated pages down from where the real issue existed.

In app shell navigation you will basically have the top level which will be added in the MauiProgram.cs like this:

builder.services.AddSingleton<MyXAMLTopPage>();
builder.services.AddSingleton<MyXAMLSecondPage>();

(or you could choose AddTransient, too I guess).

Then the rest of the pages are added in here as well.

Then in AppShell.xaml you will have the top-level menu routings:

<ShellContent
Title="My Pages"
ContentTemplate="{DataTemplate local:MyXAMLTopPage}"
Route="MyXAMLTopPage"/>
...

For the child pages they will exist in the AppShell.xaml.cs file like this:

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

I had the top-level pages listed in the AppShell.xaml.cs file in addition to the AppShell.xaml file. This caused the routing issue two pages down for me. The top-level page wasn't listed in the above error message for me.

I hope this helps someone else out; it was a pain to solve.

Zonus
  • 2,313
  • 2
  • 26
  • 48
  • Thank you Zonus. I was having the same issue and your explanation helped to find the issue. I was baffled why my second-level page was shown registered twice, when in fact I was sure it is only registered on the CS file and not in XAML. – Dinesh_Fernando Apr 11 '23 at 02:06
  • 100% This. Been tearing my hair out as I knew I didn't have the issue with a sub page being declared twice. Ended up being a page from much earlier on. Thanks @Zonus for this, great answer! – D Hansen Jun 07 '23 at 11:05