0

您好,我遇到了这个问题,我需要回到我的第一个 Shell 项目,因为我不知道怎么做,而且谷歌搜索没有帮助我在这里问。

 <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 个 shell 项目,当用户转到ExplorePage或任何TabBar我想设置后退按钮返回到ChoicePage. 我试图添加await Shell.Current.GoToAsync("..");,但没有运气,甚至PopToRootAsync() 不起作用。

4

1 回答 1

0

如果您想在某些特定页面中覆盖导航返回按钮(软按钮,而非物理按钮)的默认行为,则需要在这些页面中设置附加的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)));

如果您希望物理后退按钮的行为方式也相同,那么您需要在页面的代码隐藏、相关问题Confirmation Dialog on back button press event Xamarin.Forms中覆盖OnBackButtonPressed()

于 2021-04-06T13:16:20.913 回答