如何在 OnNavigatingFromAsync 中显示模式对话框和取消导航
How to show a Modal Dialog and Cancel Navigation in OnNavigatingFromAsync
使用 Template 10 时,您有机会通过覆盖 INavigable
方法 OnNavigatingFromAsync
并将 args.Cancel
设置为是的,像这样:
public override Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
// some logic to determine if navigation should be canceled...
args.Cancel = true;
return Task.CompletedTask;
}
这很好用,但是如果我想向用户显示模式对话框(解释为什么取消导航),我会将方法修改为:
public async override Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
args.Cancel = true;
ContentDialog dlg = new ContentDialog()
{
Title = "Bad",
Content = "no no no!",
PrimaryButtonText = "OK",
SecondaryButtonText = "NO"
};
await dlg.ShowAsync();
}
这将显示对话框,但是未取消导航。就像 T10 忽略了正在设置的 args.Cancel = true;
。
我是不是做错了什么?我只想显示对话框然后阻止导航..
我在 Hamburger Sample 的模板 10 (1.1.4) 上试用了您的模态框,效果非常好。
对我来说,我认为你的错误出在方法 "OnNavigatingFromAsync" 上,它看起来像最后缺少 "return Task.CompletedTask"。
对我来说,当我点击应用程序中的后退键时,这段代码会阻止应用程序返回:
public override Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
args.Cancel = true;
ContentDialog dlg = new ContentDialog()
{
Title = "Bad",
Content = "no no no!",
PrimaryButtonText = "OK",
SecondaryButtonText = "NO"
};
dlg.ShowAsync();
return Task.CompletedTask;
}
使用 Template 10 时,您有机会通过覆盖 INavigable
方法 OnNavigatingFromAsync
并将 args.Cancel
设置为是的,像这样:
public override Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
// some logic to determine if navigation should be canceled...
args.Cancel = true;
return Task.CompletedTask;
}
这很好用,但是如果我想向用户显示模式对话框(解释为什么取消导航),我会将方法修改为:
public async override Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
args.Cancel = true;
ContentDialog dlg = new ContentDialog()
{
Title = "Bad",
Content = "no no no!",
PrimaryButtonText = "OK",
SecondaryButtonText = "NO"
};
await dlg.ShowAsync();
}
这将显示对话框,但是未取消导航。就像 T10 忽略了正在设置的 args.Cancel = true;
。
我是不是做错了什么?我只想显示对话框然后阻止导航..
我在 Hamburger Sample 的模板 10 (1.1.4) 上试用了您的模态框,效果非常好。
对我来说,我认为你的错误出在方法 "OnNavigatingFromAsync" 上,它看起来像最后缺少 "return Task.CompletedTask"。
对我来说,当我点击应用程序中的后退键时,这段代码会阻止应用程序返回:
public override Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
args.Cancel = true;
ContentDialog dlg = new ContentDialog()
{
Title = "Bad",
Content = "no no no!",
PrimaryButtonText = "OK",
SecondaryButtonText = "NO"
};
dlg.ShowAsync();
return Task.CompletedTask;
}