在 UWP Template10 中取消导航

Cancel Navigation in UWP Template10

如何在 UWP Template 10 应用中取消导航?我们正在尝试取消 ViewModel 中存在未保存数据 (IsDirty) 的导航。

public class ViewModel : ViewModelBase
{
    public override async Task OnNavigatingFromAsync(NavigatingEventArgs args)
    {
        if (this.IsDirty)
        {
            ContentDialog errorDialog = new ContentDialog();
            errorDialog.Title = "Save?";
            errorDialog.Content = "If you continue you will lose unsaved changes";
            errorDialog.PrimaryButtonText = "Cancel";
            errorDialog.SecondaryButtonText = "Continue";
            ContentDialogResult answer = await errorDialog.ShowAsync();
            if (answer == ContentDialogResult.Primary)
                args.Cancel = true;
        }
        await Task.CompletedTask;
    }

但是导航并没有被取消。如果我们省略对话框,可以取消导航。如何取消导航离开 ViewModel 以响应对话框?

有人和你有同样的问题。 https://github.com/Windows-XAML/Template10/issues/1307

已在 1.1.13(预览)版本中解决。所以你需要更新nuget包。