c# - Xamarin.Forms:处理导航工具栏按钮(Shell)
问题描述
我想处理我的 Xamarin.Forms 应用程序(android 版本)中的任何后退点击。
对于 Android/硬件按钮非常简单,我所要做的就是重写 OnBackButtonPressed 方法。但是这个顶部工具栏并不那么容易。我听说它应该与 Shell.SetBackButtonBehavior 一起使用,但由于某种原因,它对我不起作用。
我的代码是这样的,在InitializeComponents()之后放在页面构造函数中。
Shell.SetBackButtonBehavior(this, new BackButtonBehavior {
Command = new Command(async () =>
await Shell.Current.DisplayAlert("Back","Back","Back")
)
});
知道有什么问题吗?
解决方案
该功能可以通过在xaml中定义Shell.BackButtonBehavior,在cs中实现Command来实现。
这是我的一个小例子:
在xml中:</p>
<Shell.BackButtonBehavior>
<BackButtonBehavior Command="{Binding GoBack}" >
</BackButtonBehavior>
</Shell.BackButtonBehavior>
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
在 cs 中:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page2 : ContentPage
{
public ICommand GoBack { get; set; }
public Page2()
{
InitializeComponent();
GoBack = new Command(async () =>
await Shell.Current.DisplayAlert("Back", "Back", "Back"));
BindingContext = this;
}
}
推荐阅读
- angular - ngModel 有条件时读取值时出错
- python - 检查输入时出错:预期 conv1d_195_input 有 3 个维度,但得到了形状为 (1229, 1) 的数组
- java - maven 不从命令行检测测试
- sql - 有没有办法用不同的值更新同一个表中的多行?
- postgresql - Postgresql 在 COPY-TO 时向字符串添加双引号
- msal - Microsoft 单点登录 - react-aad-msal 库 - 无法获取访问令牌
- r - 执行 R 代码后未保存全局变量
- r - R n 最相似的时间序列 - dwt 聚类/最近邻
- scala - 如何为泛型类型定义类型
- c# - WCF 如何在 WCF 中传播 DiagnosticSource.Activity 上下文?