a.p*_*alo 6 navigation xamarin xamarin.forms
有没有办法检测 Xamarin 表单中导航页面的后退按钮的按下?
您可以覆盖导航页面的“OnBackButtonPressed”方法:
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
if (await DisplayAlert("Exit?", "Are you sure you want to exit from this page?", "Yes", "No"))
{
base.OnBackButtonPressed();
await App.Navigation.PopAsync();
}
});
return true;
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是外壳,则可以覆盖外壳的 OnNavigating 事件:
void OnNavigating(object sender, ShellNavigatingEventArgs e)
{
// Cancel back navigation if data is unsaved
if (e.Source == ShellNavigationSource.Pop && !dataSaved)
{
e.Cancel();
}
}
Run Code Online (Sandbox Code Playgroud)
更新: 当用户按下硬件后退按钮时,OnBackButtonPressed 事件只会在 Android 上触发。
似乎您更感兴趣的是在任何页面消失时实现您想做的事情!在这种情况下:您有页面的两种方法 -
protected override void OnAppearing()
{
base.OnAppearing();
Console.WriteLine("Hey, Im coming to your screen");
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Console.WriteLine("Hey, Im going from your screen");
}
Run Code Online (Sandbox Code Playgroud)
您可以在任何页面上覆盖这 2 种方法以跟踪它们何时出现和消失。
归档时间: |
|
查看次数: |
4050 次 |
最近记录: |