问题
I've also posted this question to Microsoft's Documentation here.
I am trying to implement both Access Keys and Keyboard Accelerators in a UWP app, which, as I understand it, is "good coding practice". Here is a snippet from MainPage.xaml where I try to implement the "open" shortcut key of "Ctrl+O"
<MenuFlyoutItem Text="Open" AccessKey="O" Click="menuFileOpen">
<MenuFlyoutItem.KeyboardAccelerators>
<KeyboardAccelerator Modifiers="Control" Key="O" Invoked="menuFileOpen"/>
</MenuFlyoutItem.KeyboardAccelerators>
</MenuFlyoutItem>
Inside MainPage.xaml.cs is the following snippet of code:
/// <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();
}
For debugging purposes, I haven't actually implemented the code to open the file, because of the issue I'm having. All that should happen is, when I select Open from the menu, I should get "NotImplementedException" in the debugger, and the program should stop.
In short, selecting the flyout menu item with a mouse throws the exception. Using access keys also throws the exception. Pressing the Keyboard Accelerator, however, does NOTHING! It doesn't enter the "Invoked" method (the second method signature above), so I can't even place a break point to see if the method gets a hit at all...which I'm pretty certain it doesn't!
What I've tried:
- Renaming the methods to give them unique names
- Adding the "IsEnabled" property, setting it to true, on the KeyboardAccelerator tag in the XAML file.
Any assistance is very much appreciated.
来源:https://stackoverflow.com/questions/51456230/cannot-get-keyboard-accelerators-to-work-at-all