Xamarin Forms Shell - Navigation to old navigation stack rather than flyout page

依然范特西╮ 提交于 2021-01-28 09:16:01

问题


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.

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

Search & History

<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>

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)

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

Shell.Current.GoToAsync("PageA");

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

"//searchpage/PageA"

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

"//searchpage/PageA/PageB/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

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

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

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

Thanks

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


回答1:


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

  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));
    
  2. 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");
    
  3. 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.

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



来源:https://stackoverflow.com/questions/64560659/xamarin-forms-shell-navigation-to-old-navigation-stack-rather-than-flyout-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!