【问题标题】:Prevent user dismiss dialog by press ESC button通过按 ESC 按钮防止用户关闭对话框
【发布时间】:2018-09-23 07:32:27
【问题描述】:

我正在使用 UWP 并使用 ContentDialog 来显示内容。我想通过按 ESC 按钮来防止用户关闭对话框。我尝试了这个解决方案,但是当我设置 Cancel = true 时。我无法处理主按钮中的点击事件:

How to prevent the ContentDialog from closing when home key is pressed in Windows phone 8.1..?

我们有什么办法防止它发生吗?无论如何,我的目的是锁定屏幕。

【问题讨论】:

    标签: c# uwp dialog


    【解决方案1】:

    这是我的解决方案:

     dialog.Closing += DialogClosingEvent;
    
     private void DialogClosingEvent(ContentDialog sender, ContentDialogClosingEventArgs args)
     {
          // This mean user does click on Primary or Secondary button
          if(args.Result == ContentDialogResult.None)
          {
               args.Cancel = true;
          }
     }
    

    【讨论】:

      【解决方案2】:
      <Capabilities>
        <Capability Name="internetClient" />
        <rescap:Capability xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" Name="inputForegroundObservation"/>
      </Capabilities>
      

      在您的应用程序中添加上述权限。在 Appxmanifest 文件中。添加上述代码后,您的清单文件可能无法正常打开。因此,您可以使用 XML 编辑器打开。

      Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
      
      private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
          {
              if(args.key == Windows.System.VirtualKey.Esc)
              {
                 args.Handled = true;
              }
          }
      

      以上代码将禁用整个应用程序中的 ESC 按钮。

      【讨论】:

        【解决方案3】:

        如果Form.CancelButton 属性设置为按钮控件(很可能是您所说的主按钮),则可以通过按 ESC 关闭表单对话框。

        此外,如果设置了Button.DialogResult,对话框将通过按下按钮关闭。

        您可能需要检查主按钮 DialogResult 属性。

        【讨论】:

          【解决方案4】:

          您可以尝试这种解决方法,但我认为还有另一种更好的方法:

          在 MainPage.xaml.cs 上:

          private async void ShowDialog()
          {
             SampleDialog sampleDialog = new SampleDialog();
             await sampleDialog.ShowAsync();
          }
          

          在 SampleDialog.xaml.cs 上:

              bool isEscape;
          
              public SampleDialog()
              {
                  isEscape = true;
                  this.InitializeComponent();
                  this.Closing += SampleDialog_Closing;
              }
          
              private void SampleDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
              {
                  if (isEscape)
                      args.Cancel = true;
              }
          
              private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
              {
                  this.Title = "Primary button clicked";
              }
          
              private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
              {
                  isEscape = false;
                  this.Hide();
              }
          

          【讨论】:

            猜你喜欢
            • 2015-01-08
            • 2012-08-22
            • 1970-01-01
            • 2010-11-03
            • 1970-01-01
            • 1970-01-01
            • 2020-05-05
            • 1970-01-01
            • 2022-01-06
            相关资源
            最近更新 更多