显示确认对话框时如何设置x按钮不可点击?

时间:2018-03-23 01:49:08

标签: c# uwp windows-10

当我单击x按钮应用程序显示确认对话框时,如果我第二次单击x按钮,则会发生错误,因为应用程序无法同时创建2个对话框。
目前我可以通过使用变量isBtnCloseClicked忽略第二个x按钮点击事件来保存x的状态,只在isBtnCloseClicked=false时创建对话框。但我不想在每个窗口中注册状态变量。是否有其他方法可以使用默认的UWP API执行此操作?例如:在对话框出现时禁用x按钮 下面是我处理x按钮事件的代码。

 Windows.UI.Core.Preview.SystemNavigationManagerPreview.GetForCurrentView().CloseRequested +=
            async (sender, args) =>
            {
                args.Handled = true;
                    ContentDialog locationPromptDialog = new ContentDialog
                    {
                        Title = "Do you want to exit?",
                        Content = "",
                        CloseButtonText = "No",
                        PrimaryButtonText = "Yes"
                    };
                    ContentDialogResult result = await locationPromptDialog.ShowAsync();
                    args.Handled = false;
                    if (result == ContentDialogResult.Primary)
                    {
                        App.Current.Exit();
                    }              
            };

第二次点击x按钮时出错

System.Runtime.InteropServices.COMException
  Message=An async operation was not properly started.

Only a single ContentDialog can be open at any time.
  Source=Windows
  StackTrace:
   at Windows.UI.Xaml.Controls.ContentDialog.ShowAsync()
   at CORsvr.MainPage. 

谢谢

1 个答案:

答案 0 :(得分:1)

这应该适合你。

Windows.UI.Core.Preview.SystemNavigationManagerPreview.GetForCurrentView().CloseRequested +=
            async (sender, args) =>
            {
                args.Handled = true;

                var messageDialog = new MessageDialog("Do you want to exit?");


                messageDialog.Commands.Add(new UICommand(
                    "OK", 
                    new UICommandInvokedHandler(this.OKCommandInvokedHandler)));
                messageDialog.Commands.Add(new UICommand(
                    "Cancel", 
                    new UICommandInvokedHandler(this.CancelCommandInvokedHandler)));


                messageDialog.DefaultCommandIndex = 0;
                messageDialog.CancelCommandIndex = 1;

                await messageDialog.ShowAsync();
            };

private void OKCommandInvokedHandler(IUICommand command)
{

}

private void CancelCommandInvokedHandler(IUICommand command)
{

}