导航回到第一个 shell 项

Navigate Back to first shell item

你好,我遇到了这个问题,我需要回到我的第一个 Shell 项目,因为我不知道如何做,而且 google 搜索没有帮助我问这里。

 <ShellItem Route="ChoicePage" >
    <ShellContent ContentTemplate="{DataTemplate local:ChoicePage}" />
</ShellItem>
<ShellItem Route="LoginPage" >
    <ShellContent ContentTemplate="{DataTemplate local:LoginPage}" />
</ShellItem>
<TabBar Route="MainPage">
    <Tab Title="{helpers:Translate Explore}" 
         Icon="explore.png" 
         Route="ExplorePage">
        <ShellContent ContentTemplate="{DataTemplate local:ExplorePage}" />
    </Tab>
    <Tab Title="{helpers:Translate Plan}" 
         Icon="globe.png" 
         Route="NextTripsPage">
        <ShellContent ContentTemplate="{DataTemplate local:NextTripsPage}" />
    </Tab>
    <Tab Title="{helpers:Translate Trips}" 
         Icon="around.png" 
         Route="MyTripsPage">
        <ShellContent ContentTemplate="{DataTemplate local:MyTripsPage}" />
    </Tab>
    <Tab
        Title="{helpers:Translate Map}"
        Icon="map.png"
        Route="MyMap">
        <ShellContent ContentTemplate="{DataTemplate local:FullMapPage }" />
    </Tab>
    <Tab
        Title="{helpers:Translate MeTextProfile}"
        Icon="me.png"
        Route="MyProfilePage">
        <ShellContent ContentTemplate="{DataTemplate local:MyProfilePage}" />
    </Tab>
</TabBar>

我有这 2 个外壳项目,当用户转到 ExplorePage 或任何 TabBar 时,我想将后退按钮设置为返回 ChoicePage。我尝试添加 await Shell.Current.GoToAsync(".."); 但没有运气,甚至 PopToRootAsync() 也不起作用。

如果您想在某些特定页面中覆盖导航后退按钮(软按钮,而非物理按钮)的默认行为,那么您需要在这些页面中设置附加的 Shell.BackButtonBehavior

ExplorePage.xaml

<ContentPage ...>    
    <Shell.BackButtonBehavior>
        <BackButtonBehavior Command="{Binding BackCommand}"/>   
    </Shell.BackButtonBehavior>
    ...
</ContentPage>

视图模型

public Command BackCommand { get; set; } =  new Command(
         async () => await Shell.Current.GoToAsync(nameof(ChoicePage)));

如果您希望物理后退按钮也以同样的方式运行,那么您需要重写 OnBackButtonPressed() in your page's code-behind, related question