gpt4 book ai didi

xamarin - 检测 Xamarin 表单中导航页面的后退箭头按下

转载 作者:行者123 更新时间:2023-12-03 16:22:52 26 4
gpt4 key购买 nike

有没有办法检测 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;
}

如果您使用的是 shell ,则可以覆盖 shell 的 OnNavigating 事件:
void OnNavigating(object sender, ShellNavigatingEventArgs e)
{
// Cancel back navigation if data is unsaved
if (e.Source == ShellNavigationSource.Pop && !dataSaved)
{
e.Cancel();
}
}

更新:
当用户按下硬件后退按钮时,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");
}

您可以在任何页面上覆盖这 2 种方法以跟踪它们何时出现和消失。

关于xamarin - 检测 Xamarin 表单中导航页面的后退箭头按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57662491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com