【发布时间】:2021-08-16 06:38:31
【问题描述】:
我正在尝试创建我的第一个 Xamarin 移动应用程序,在使用返回按钮 (android) 导航回上一个屏幕/选项卡时我有点困惑。我创建了一个测试项目,并使用 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 中使用导航堆栈,但不知道如何将它与选项卡一起使用。
感谢您的宝贵时间。
干杯
编辑
-定义层次结构的 AppShell 中的代码
<?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>
【问题讨论】:
-
你为什么要覆盖后退按钮 - 如果你不覆盖会发生什么?您为将选项卡集定义为 Shell 层次结构的一部分而编写的 XAML 是什么?或者对于一组简单的选项卡,可以使用
TabbedPage而不是 Shell。 -
如果我不覆盖后退按钮,它会在按下后退按钮时关闭应用程序。我将发布在 shell 中显示层次结构的代码。我也在考虑只使用 tabbedpage 而不是 Shell。感谢您的评论
标签: xamarin xamarin.forms xamarin.android