使用后退按钮在选项卡之间导航

Navigate between tabs using the back button

我正在尝试创建我的第一个 Xamarin 移动应用程序,在使用后退按钮 (android) 导航回之前的 screen/tab 时我有点困惑。我创建了一个测试项目,我正在使用 Xamarin 表单 shell 来定义层次结构。 我使用的导航类型是 Tab。在我的测试应用程序中,我有 2 个选项卡

这是我当前的方法,如果用户按下后退按钮,我将使用该方法从第二个选项卡导航回第一个选项卡。 我重写 OnBackButtonPressed 方法并执行执行 GoTo 方法的命令。

    public partial class ItemsPage : ContentPage
        {
            ItemsViewModel _viewModel;
            public ICommand goBack; 
            public ItemsPage()
            {
                InitializeComponent();
                BindingContext = _viewModel = new ItemsViewModel();
                goBack = new Command(async (x) => await Shell.Current.GoToAsync("////AboutPage"));
            }
            protected override void OnAppearing()
            {
                base.OnAppearing();
                _viewModel.OnAppearing();           
            }
            protected override bool OnBackButtonPressed()
            {
                base.OnBackButtonPressed();
                goBack.Execute(null);
                return true;
            }
        }

这行得通,但问题是,我已经对要返回的页面进行了硬编码。将来,我将再添加 2 个选项卡,所以在这种情况下,例如,如果用户在第 4 个选项卡上,而前一个选项卡是选项卡 3,我如何检索该信息以便我可以成功地导航用户返回到上一个选项卡或页面? 我尝试在 Shell 中使用导航堆栈,但无法弄清楚如何将其与选项卡一起使用。

感谢您的宝贵时间。

干杯

编辑

-App 中的代码Shell 定义层次结构

    <?xml version="1.0" encoding="UTF-8"?>
    <Shell xmlns="http://xamarin.com/schemas/2014/forms" 
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
           xmlns:local="clr-namespace:TestApp03.Views"
           Title="TestApp03"
           x:Class="TestApp03.AppShell">
        <TabBar>
            <ShellContent Title="About" Icon="icon_about.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
            <ShellContent Title="Browse" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:ItemsPage}" />
        </TabBar>
    </Shell>

您可以使用await Shell.Current.GoToAsync("..");实现前跳。更多用法请参考这个link:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/tabs