c# - 在 UserControl 中捕获 Esc 键

标签 c# xaml uwp

我想使用 UserControl 创建一个自定义弹出窗口,因为这是 UWP 应用,我想隐藏 Popup code> 当用户按键盘上的 ESC 时。

我尝试重写UserControlOnKeyDown方法,但当我在键盘上按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/

相关文章:

uwp - 如何设置全屏闪屏图像

c# - 有没有办法内联初始化异常的数据?

c# - asp.net 图表控件中带有日期时间 x 轴的数据点

xaml - 单击按钮时触发 Storyboard?

wpf - 根据 DataContext 的类型选择用户控件

c# - 它的选中状态是怎么去掉的?

android - 在 IOS 中使用框架阴影效果显示,但在 xamarin 中的 UWP 项目中则不使用

c# - GetPrintJobInfoCollection() 有时会出现异常

c# - 通过 System.Reflection 访问内部成员?

xaml - WinRT 中的数据触发器?