我想使用 UserControl
创建一个自定义弹出窗口
,因为这是 UWP
应用,我想隐藏 Popup
code> 当用户按键盘上的 ESC
时。
我尝试重写UserControl
的OnKeyDown
方法,但当我在键盘上按ESC
时,此方法从未执行。
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Escape)
{
this.Visibility = Visibility.Colapse;
}
}
最佳答案
I want to hide Popup when user press ESC on Keyboard.
在 UWP 应用中,考虑使用 CoreWindow.CharacterReceived event
在 UserControl 中,在构造函数方法中添加事件处理程序:
public CustomPopupControl()
{
this.InitializeComponent();
Window.Current.CoreWindow.CharacterReceived += CoreWindow_CharacterReceived;
}
private void CoreWindow_CharacterReceived(CoreWindow sender, CharacterReceivedEventArgs args)
{
if(args.KeyCode==27) //ESC
{
//Do somthing
this.Visibility = Visibility.Collapsed;
}
}
关于c# - 在 UserControl 中捕获 Esc 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34371280/