代码之家  ›  专栏  ›  技术社区  ›  slayernoah

e、 在UWP中等待异步对话框后无法设置取消

  •  0
  • slayernoah  · 技术社区  · 7 年前

    这是针对Windows 10 UWP应用程序的。当用户试图离开页面时,我想让用户确认是否要保存当前数据。

    我已覆盖 OnNavigatingFrom 如下所示。但是,在async MessageDialog之后 e.Cancel=false 不起作用。该页面仍保留在当前页面上,即使 e.Cancel 稍后设置为false。请帮忙!

    protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
    
    {
        e.Cancel = true; //if I don't put this at the top, the page navigates right away
    
        var yesCommand = new UICommand("Yes", async cmd => {
    
            try
            {
                await SaveWorkshetItem(false);
                e.Cancel = false;
            }
            catch (Exception ex)
            {
                await new MessageDialog("Error saving Worksheet Item. Please contact you administrator." + ex.Message + Environment.NewLine + ex.StackTrace).ShowAsync();
            }
    
        });
    
        var noCommand = new UICommand("No", cmd => { e.Cancel = false; });
    
        var cancelCommand = new UICommand("Cancel", cmd => { e.Cancel = true;  });
    
        var dialog = new MessageDialog("Do you want to save the current item before navigating away?");
        dialog.Options = MessageDialogOptions.None;
        dialog.Commands.Add(yesCommand);
    
        dialog.Commands.Add(noCommand);
        dialog.Commands.Add(cancelCommand);
    
        await dialog.ShowAsync();
    
        base.OnNavigatingFrom(e);
    
    }
    

    为了简化这一点,下面的代码使页面永远不会离开,即使我正在更改回 e、 取消=错误 样品之后 MessageDialog

    protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
    
    {
        e.Cancel = true; //if I don't put this at the top, the page navigates right away
    
        await new MessageDialog("Do you want to save the current item before navigating away?").ShowAsync();
    
        e.Cancel = false;  //unconditionally setting this back to false and it still won't leave the page
    
        base.OnNavigatingFrom(e);
    }
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   Stefan Wick MSFT    7 年前

    要自己处理导航,请设置Cancel=true(正如您已经做的那样),然后打开对话框以获取用户输入。一旦知道用户的选择,如果用户决定允许进行导航,请使用导航API(例如Frame.GoBack)执行所需的导航(基于例如NavigationMode)。

    以下是一些基本示例代码:

    private bool isNavigationConfirmed = false;
    protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        base.OnNavigatingFrom(e);
        if (isNavigationConfirmed)
        {
            isNavigationConfirmed = false;
            return;
        }
        e.Cancel = true;
    
        var noCommand = new UICommand("No", cmd => { });
        var yesCommand = new UICommand("Yes", cmd =>
        {
            if (e.NavigationMode == NavigationMode.Back)
            {
                Frame.GoBack();
            }
            else
            {
                isNavigationConfirmed = true;
                Frame.Navigate(e.SourcePageType);
            }
        });
    
        var dialog = new MessageDialog("Do you want to allow navigation?");
        dialog.Options = MessageDialogOptions.None;
        dialog.Commands.Add(yesCommand);
        dialog.Commands.Add(noCommand);
        await dialog.ShowAsync();
    }