我也已将这个问题发布到 Microsoft 的文档here https://learn.microsoft.com/en-us/windows/uwp/design/input/keyboard-accelerators#custom-keyboard-accelerator-behavior.
我正在尝试在 UWP 应用程序中实现访问键和键盘加速器,据我所知,这是“良好的编码实践”。这是 MainPage.xaml 中的一个片段,我尝试在其中实现“Ctrl+O”的“打开”快捷键
<MenuFlyoutItem Text="Open" AccessKey="O" Click="menuFileOpen">
<MenuFlyoutItem.KeyboardAccelerators>
<KeyboardAccelerator Modifiers="Control" Key="O" Invoked="menuFileOpen"/>
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
MainPage.xaml.cs 内部是以下代码片段:
/// <summary>
/// Menu option to select a file
/// </summary>
private void menuFileOpen(object sender, RoutedEventArgs e)
{
this.OpenFile();
}
/// <summary>
/// Keyboard Accelerator to select a file
/// </summary>
private void menuFileOpen(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
this.OpenFile();
args.Handled = true; // because the docs say to do this
}
/// <summary>
/// Opens the dialog box to select a file.
/// </summary>
private void OpenFile()
{
// TODO: write the code to open the file
throw new NotImplementedException();
}
出于调试目的,由于遇到问题,我实际上尚未实现打开文件的代码。应该发生的事情是,当我从菜单中选择“打开”时,我应该在调试器中收到“NotImplementedException”,并且程序应该停止。
简而言之,用鼠标选择弹出菜单项会引发异常。使用访问键也会引发异常。然而,按下键盘加速键没有任何作用!它不会进入“Invoked”方法(上面的第二个方法签名),所以我什至无法放置一个断点来查看该方法是否完全命中......我很确定它没有命中t!
我尝试过的:
- 重命名方法以赋予它们唯一的名称
- 在 XAML 文件中的 KeyboardAccelerator 标记上添加“IsEnabled”属性,并将其设置为 true。
非常感谢任何帮助。
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)