(UWP) 使用 ESC 键关闭 ModalDialog
(UWP) Using ESC Key to Close ModalDialog
我在 Template10
中使用示例代码 Busy.xaml
显示 ModalDialog
:
public static void SetBusy(bool busy, string text = null)
{
WindowWrapper.Current().Dispatcher.Dispatch(() =>
{
var modal = Window.Current.Content as ModalDialog;
var view = modal.ModalContent as Busy;
if (view == null)
modal.ModalContent = view = new Busy();
modal.IsModal = view.IsBusy = busy;
view.BusyText = text;
modal.CanBackButtonDismiss = true;
});
}
我可以使用 ALT+Left Arrow
关闭此对话框,但在大多数桌面应用程序中,按 ESC
键通常也会关闭弹出窗口或对话框。
我尝试添加代码来处理 Busy.xaml
上的 KeyDown
但是当我按下 ESC
或任何键时此方法从未执行。
private void UserControl_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Escape)
{
e.Handled = true;
SetBusy(false);
}
}
那么,如何让这个 ModalDialog
在用户按下 ESC
键时关闭?
当模式打开时,只需沿着这条路线使用一些东西:
private void Modal_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
另一种解决 (e.KeyCode==Keys.Escape)
的方法是:
(e.KeyChar == (char)27)
或
e.KeyCode==(char)Keys.Escape
要使此代码生效,您需要 Form.KeyPreview = true;
有关以上内容的更多信息:https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
我认为您需要附加 CancelButton
属性 才能正常工作。
(几乎相同的方法)我相信这应该也能很好地工作:
private void HandleEsc(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
Close();
}
这是一个控制台应用程序:
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
return;
}
您必须将事件处理程序附加到 CoreWindow
的 CharacterReceived
事件。
修改SetBusy
方法:
public static void SetBusy(bool busy, string text = null)
{
WindowWrapper.Current().Dispatcher.Dispatch(() =>
{
var modal = Window.Current.Content as ModalDialog;
var view = modal.ModalContent as Busy;
if (view == null)
modal.ModalContent = view = new Busy();
modal.IsModal = view.IsBusy = busy;
view.BusyText = text;
modal.CanBackButtonDismiss = true;
// Attach to key inputs event
var coreWindow = Window.Current.CoreWindow;
coreWindow.CharacterReceived += CoreWindow_CharacterReceived;
});
}
CoreWindow_CharacterReceived
看起来像这样:
private static void CoreWindow_CharacterReceived(CoreWindow sender,
CharacterReceivedEventArgs args)
{
// KeyCode 27 = Escape key
if (args.KeyCode != 27) return;
// Detatch from key inputs event
var coreWindow = Window.Current.CoreWindow;
coreWindow.CharacterReceived -= CoreWindow_CharacterReceived;
// TODO: Go back, close window, confirm, etc.
}
我在 Template10
中使用示例代码 Busy.xaml
显示 ModalDialog
:
public static void SetBusy(bool busy, string text = null)
{
WindowWrapper.Current().Dispatcher.Dispatch(() =>
{
var modal = Window.Current.Content as ModalDialog;
var view = modal.ModalContent as Busy;
if (view == null)
modal.ModalContent = view = new Busy();
modal.IsModal = view.IsBusy = busy;
view.BusyText = text;
modal.CanBackButtonDismiss = true;
});
}
我可以使用 ALT+Left Arrow
关闭此对话框,但在大多数桌面应用程序中,按 ESC
键通常也会关闭弹出窗口或对话框。
我尝试添加代码来处理 Busy.xaml
上的 KeyDown
但是当我按下 ESC
或任何键时此方法从未执行。
private void UserControl_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Escape)
{
e.Handled = true;
SetBusy(false);
}
}
那么,如何让这个 ModalDialog
在用户按下 ESC
键时关闭?
当模式打开时,只需沿着这条路线使用一些东西:
private void Modal_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
另一种解决 (e.KeyCode==Keys.Escape)
的方法是:
(e.KeyChar == (char)27)
或
e.KeyCode==(char)Keys.Escape
要使此代码生效,您需要 Form.KeyPreview = true;
有关以上内容的更多信息:https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
我认为您需要附加 CancelButton
属性 才能正常工作。
(几乎相同的方法)我相信这应该也能很好地工作:
private void HandleEsc(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
Close();
}
这是一个控制台应用程序:
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
return;
}
您必须将事件处理程序附加到 CoreWindow
的 CharacterReceived
事件。
修改SetBusy
方法:
public static void SetBusy(bool busy, string text = null)
{
WindowWrapper.Current().Dispatcher.Dispatch(() =>
{
var modal = Window.Current.Content as ModalDialog;
var view = modal.ModalContent as Busy;
if (view == null)
modal.ModalContent = view = new Busy();
modal.IsModal = view.IsBusy = busy;
view.BusyText = text;
modal.CanBackButtonDismiss = true;
// Attach to key inputs event
var coreWindow = Window.Current.CoreWindow;
coreWindow.CharacterReceived += CoreWindow_CharacterReceived;
});
}
CoreWindow_CharacterReceived
看起来像这样:
private static void CoreWindow_CharacterReceived(CoreWindow sender,
CharacterReceivedEventArgs args)
{
// KeyCode 27 = Escape key
if (args.KeyCode != 27) return;
// Detatch from key inputs event
var coreWindow = Window.Current.CoreWindow;
coreWindow.CharacterReceived -= CoreWindow_CharacterReceived;
// TODO: Go back, close window, confirm, etc.
}