So I have a Button in Xaml:
<Button x:Name="btnCancel"
Click="btnCancel_Click"
Content="Cancel"></Button>
and in a ContentDialog I want to press a Key to trigger that button. My Problem is that no key presses are recognized and I don´t understand why.
.cs:
public sealed partial class MyDialog : ContentDialog
{
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
this.KeyDown += onE_KeyDown;
}
//...
private void onE_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
}
}
I put a breakpoint at the onE_KeyDown
method but it never gets there.
答案 0 :(得分:1)
如果KeyDown事件从不触发,则该控件很可能在内部对其进行处理。 MSDN说:
特定的Windows运行时控件可能对KeyDown输入事件具有基于类的处理。如果是这样,则控件可能会覆盖方法OnKeyDown。通常,这些类处理程序旨在处理按键的子集,以使基于键盘的用户与该控件进行交互,并且这种交互通常支持键盘可访问性功能。如果某个按键是通过基于类的处理来处理的,则认为该按键已被处理,并且该控件上的任何用户代码处理程序都不会引发KeyDown事件来专门处理该按键。
在这种情况下,您可以尝试覆盖OnKeyDown method来更改此行为。不幸的是,我现在无法检查这是否真的是问题所在,但这也许会对您有所帮助。
答案 1 :(得分:0)
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
//this.KeyDown += MyDialog_KeyDown;
此外,如果您不考虑长时间不释放键盘的情况,请尝试使用可以正常触发的 KeyUp事件 。< / p>
顺便说一句,默认情况下,键盘的Esc键将关闭ContentDialog ,因此您需要在Closing事件中首先阻止此功能。
比赛代码:
public MyDialog()
{
this.InitializeComponent();
this.Loaded += MyDialog_Loaded;
this.Closing += ContentDialog_Closing;
//this.KeyDown += onE_KeyDown;
this.KeyUp += MyDialog_KeyUp;
}
private void MyDialog_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
//btnCancel_Click(sender, e);
btnCancel_Click(this, new RoutedEventArgs());
}
}
void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
//args.Cancel = syncInProcess;
// btnCancel_Click(this, new RoutedEventArgs());
if (args.Result == ContentDialogResult.None)
{
args.Cancel = true;
}
}
答案 2 :(得分:0)
您要查找的事件是 PreviewKeyDown 事件。如果系统默认处理该特定键,则不会触发Keydown事件。在所有情况下都会触发PreviewKeyDown。
注意:请确保您为其余键设置e.handled = false
https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.previewkeydown
<ContentDialog
x:Class="App1.NewContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="TITLE"
PreviewKeyDown="ContentDialog_PreviewKeyDown"
PrimaryButtonText="Button1"
SecondaryButtonText="Button2"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
<Grid>
</Grid>
</ContentDialog>
private void ContentDialog_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
btnCancel_Click(this, new RoutedEventArgs());
}
else
{
e.Handled = false;
}
}