如何在UWP中的"Enter"上关闭ContentDialog

Mar*_*ter 2 c# uwp

我一直在试图让一个简单的ContentDialogTextBox关闭当用户点击Enter,同时仍然在TextBox.可悲的是TextBox,即使ContentDialog响应,它甚至没有a也无法工作Esc.

我希望有一种方法可以在KeyDownHandler中设置一个Result TextBox,但是看起来ContentDialog缺少这个?!

mar*_*nax 5

您可以Hide()TextBox KeyDown处理程序中使用方法关闭ContentDialog ,简单示例:

ContentDialog c = new ContentDialog();

var tb = new TextBox();

tb.KeyDown += (sender, args) =>
{
     if (args.Key == VirtualKey.Enter)
     {
          c.Hide();
     }
};

c.Content = tb;
c.ShowAsync();
Run Code Online (Sandbox Code Playgroud)

编辑: 但是当你想要关闭对话框时,它似乎更复杂TextBox.您必须订阅全球Window.Current.CoreWindow.KeyDown活动:

ContentDialog c = new ContentDialog();

Window.Current.CoreWindow.KeyDown += (sender, args) =>
{
      if (args.VirtualKey == VirtualKey.Enter)
      {
            c.Hide();
      }
};
c.ShowAsync();
Run Code Online (Sandbox Code Playgroud)