【发布时间】:2020-05-19 08:02:42
【问题描述】:
我有一个事件会在按下 Android 硬件后退按钮时触发。这是它在 AppShell 类中的实现方式:
public event EventHandler<CancelEventArgs> BackButtonPressed;
protected override bool OnBackButtonPressed()
{
var cancelArgs = new CancelEventArgs();
BackButtonPressed?.Invoke(this, cancelArgs);
return (cancelArgs.Cancel) ? true : base.OnBackButtonPressed();
}
在其他地方,我在视图模型中订阅了此事件。代码如下:
private async void _AppShell_BackButtonPressed(object sender, CancelEventArgs e)
{
if (!await App.Current.MainPage.DisplayAlert(
"My App",
"Are you sure you want to cancel, you have unsaved changes",
"ok",
"cancel"))
{
e.Cancel = true;
}
}
在 DisplayAlert 之前还发生了一些其他事情,但为简单起见,我已将其全部删除。
问题在于,当调用 DisplayAlert 时,执行返回到 OnBackButtonPressed,因此未相应设置取消参数。因此,DisplayAlert 似乎没有等待用户响应。这个问题怎么解决?
抱歉,如果我不清楚,我可以提供进一步的澄清.. 只是问:)
对此的任何帮助将不胜感激。
【问题讨论】: