2

使用模板 10时,您有机会通过覆盖该INavigable方法OnNavigatingFromAsync并设置args.Cancel为 true 来取消页面的 ViewModel 导航离开页面,如下所示:

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;设置一样。

我在这里做错了吗?我只想显示对话框然后阻止导航..

4

1 回答 1

2

我在汉堡样本上尝试了您在模板 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;
        }
于 2016-02-23T22:53:32.267 回答