问题
I want to create a Custom Popup using UserControl, because this is UWP apps, I want to hide Popup when user press ESC on Keyboard.
I try to override OnKeyDown method of UserControl but this method never executed when I press ESC on Keyboard.
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Escape)
{
this.Visibility = Visibility.Colapse;
}
}
回答1:
I want to hide Popup when user press ESC on Keyboard.
In UWP app, consider using CoreWindow.CharacterReceived event
In UserControl, add event handler in the Constructor method:
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;
}
}
来源:https://stackoverflow.com/questions/34371280/capture-esc-key-in-usercontrol