【问题标题】:Navigate Back to first shell item导航回第一个外壳项目
【发布时间】:2021-07-02 06:19:22
【问题描述】:

您好,我遇到了这个问题,我需要回到我的第一个 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() 也不起作用。

【问题讨论】:

    标签: c# xaml xamarin.forms xamarin.forms.shell


    【解决方案1】:

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

    【讨论】:

    • 我这样做了,但由于它是一个标签页,标签栏仍然可见,我需要回到标签栏不显示的开头。
    • 很抱歉没有回复这个帖子。我接受了@Cfun 的回答。
    猜你喜欢
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多