Xamarin Forms Shell - 导航到旧导航堆栈而不是弹出页面 [英] Xamarin Forms Shell - Navigation to old navigation stack rather than flyout page

查看:46
本文介绍了Xamarin Forms Shell - 导航到旧导航堆栈而不是弹出页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Xamarin Forms Shell 应用出现问题.我不确定这是错误还是预期的行为,有人可以指出我正确的方向.

I’m having trouble with my Xamarin Forms Shell app. I'm not sure if this is a bug or expected behaviour, can someone point me in the right direction.

我有一个在 Visual Shell Hierarchy 中有 2 个页面的应用:

I have an app with 2 pages in the Visual Shell Hierarchy:

搜索&历史

<FlyoutItem Title="Search" Icon="search.png">
    <Tab Title="Search">
        <ShellContent Route="searchpage">
            <views:SearchPage />
        </ShellContent>
    </Tab>
</FlyoutItem>
<FlyoutItem Title="History" Icon="history.png">
    <Tab Title="History">
        <ShellContent>
            <views:HistoryPage />
        </ShellContent>
    </Tab>
</FlyoutItem>

还有几个页面(我们称它们为 PageA、PageB 和 PageC)是这样注册的:

And several pages (let’s call them PageA, PageB and PageC) registered like so:

Routing.RegisterRoute("PageA", typeof(PageA));    
Routing.RegisterRoute("PageB", typeof(PageB));    
Routing.RegisterRoute("PageC", typeof(PageC)); (Oops, I should probably use nameof here)

无论如何,我从搜索"页面开始,然后像这样导航到PageA":

Anyway, I start on the Search page and navigate to PageA like so:

Shell.Current.GoToAsync("PageA");

因为 PageA 不在视觉层次结构中,这给了我一个像这样的导航堆栈:

Because PageA is not in the visual hierarchy, this gives me a navigation stack like so:

"//searchpage/PageA"

使用相同的相对导航方法,我导航到 PageB 然后是 PageC,所以我的导航堆栈是这样的:

Using the same relative navigation approach, I navigate to PageB then PageC, so my navigation stack is like so:

"//searchpage/PageA/PageB/PageC"

从 PageC,我使用弹出菜单导航到历史记录,历史记录页面打开正常.

From PageC, I use the flyout menu to navigate to History, the history page opens fine.

现在在历史页面上,我再次使用弹出菜单并单击搜索"选项卡

Now on the history page, I use the flyout menu again and click the Search tab

但我没有按预期进入搜索页面,而是被带回到 PageC(带回到我之前的导航堆栈).

But I'm not taken to the search page as expected, I'm taken back to PageC (taken back to my previous navigation stack).

从此页面 (PageC) 中,如果我再次使用弹出菜单并单击搜索"选项卡,它会正确导航到搜索"页面.

From this page (PageC) if I use the flyout menu again and click the Search tab, it navigates correctly to the Search page.

当我从弹出窗口中选择搜索"选项卡时,这应该如何工作以及如何阻止它导航到 PageC?

How should this work and how can I stop it navigating to PageC when I select the Search tab from the flyout?

谢谢

(ps - 我目前使用的是 Xamarin Forms 4.7)

(ps - I'm using Xamarin Forms 4.7 at present)

推荐答案

我将在 5 分钟内将此作为功能请求提出,但我已找到此问题的解决方案.

I will be raising this as a feature request when I get 5 minutes, but I have found a solution to this issue.

  1. 我将最后一页 (PageC) 设为根页面并将其从我的路由脚本中删除.我通过将以下内容添加到 AppShell.Xaml 来做到这一点,如下所示:

  1. I made the last page (PageC) a Root page and removed it from my Routes Script. I did this by adding the following to the AppShell.Xaml like so:

<ShellItem Route="PageC">
    <ShellContent ContentTemplate="{DataTemplate views:PageC}" />
</ShellItem>

and removing this code:
Routing.RegisterRoute("PageC", typeof(PageC));

  • 现在导航到 PageC 时,我不再使用相对导航推入堆栈,我现在导航到根页面,如下所示:

  • Now when navigating to PageC, I am no longer pushing onto the stack with a relative navigation, I am now navigating to a root page like so:

     await Shell.Current.GoToAsync("//PageC");
    

  • 在导航到 PageC 之前,我们需要清除当前导航堆栈.我尝试过 PopToRootAsync,但这在导航到 PageC 之前显示了 SearchPage.我发现以下代码有效:

  • Before we navigate to PageC, we need to clear the current navigation stack. I tried PopToRootAsync, but this showed the SearchPage before navigating to PageC. I found the following code works:

         // FYI - Navigation Stack: //SearchPage/PageA/PageB
    
         var pageB = Shell.Current.Navigation.NavigationStack[2];
         var pageA = Shell.Current.Navigation.NavigationStack[1];
    
         Shell.Current.Navigation.RemovePage(pageB);
         Shell.Current.Navigation.RemovePage(pageA);
    
         // Now navigate to our route page, PageC.
         await Shell.Current.GoToAsync("//PageC");
    

  • 我将使用稍微更优雅的方式来获取页面,而不是对索引进行硬编码,但我认为这是演示正在发生的事情的最简单方法.

    I will be using a slightly more elegant way of getting the pages rather than hard coding the index, but thought this was the easiest way to demonstrate what was happening.

    下次我现在导航到 SearchPage 时,我会得到 SearchPage

    The next time I navigate to the SearchPage now, I get the SearchPage

    这篇关于Xamarin Forms Shell - 导航到旧导航堆栈而不是弹出页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆