简体   繁体   English

使用 3 个或更多页面时,GotoAsync 内置后退按钮不会 go 返回上一页

[英]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在第 3 页上,通过按后退按钮构建,它会返回到第 2 页。到目前为止一切顺利

But, On Page#4 , by pressing build in back button, it doesnt go back to Page#3.但是,在 Page#4上,通过按后退按钮构建,它不会 go 返回到 Page#3。 Why not?为什么不?

here is log: according to logs, issue is in Page#3这是日志:根据日志,问题在第 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第#1 页:我添加了//因为我希望在第#2 页中使用 ShellApp 内置菜单,而不是后退按钮

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

Page#2: Shell.Current.CurrentState.Location here is \\Page2 Page#2: Shell.Current.CurrentState.Location这里是\\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 Page#3: Shell.Current.CurrentState.Location这里是\\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 Page#4: Shell.Current.CurrentState.Location这里是\\Page2\Page3 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.我知道 GoToAsync 的工作原理是将页面堆叠在顶部并弹出。 in my code above, from page#2, I am starting to stack up pages so going back should work在我上面的代码中,从第 2 页开始,我开始堆叠页面,因此返回应该可以

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#.当您在 XAML(在 appshell 文件中)中注册您的路线并且您还尝试在 C# 中的代码中注册您的路线时,会发生这种情况。 Only register your routes once using either XAML or C# not both.仅使用 XAML 或 C# 而非两者都注册一次您的路线。

In my case, I had Page2 register twice.就我而言,我让 Page2 注册了两次。 i removed in c# and it worked for me我在 c# 中删除了它对我有用

user15900757 is right but the answer is not clear... user15900757 是对的,但答案不清楚...

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:在应用程序 shell 导航中,您基本上将拥有顶层,它将像这样添加到 MauiProgram.cs 中:

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

(or you could choose AddTransient, too I guess). (或者我猜你也可以选择 AddTransient)。

Then the rest of the pages are added in here as well.然后页面的 rest 也被添加到这里。

Then in AppShell.xaml you will have the top-level menu routings:然后在 AppShell.xaml 中,您将拥有顶级菜单路由:

<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:对于子页面,它们将存在于 AppShell.xaml.cs 文件中,如下所示:

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.除了 AppShell.xaml 文件之外,我还在 AppShell.xaml.cs 文件中列出了顶级页面。 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.解决起来很痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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