简体   繁体   中英

GotoAsync build in back button does not go back to previous page, when working with 3 or more pages

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM