问题
I'm trying to add a keyboard accelerator to a CommandBar menu item in a UWP app. This works fine when the app starts up, but after I open the overflow menu for the first time, the accelerator stops working. This doesn't appear to happen with primary commands (outside the menu), only secondary commands inside the overflow menu. Also, the menu item still works fine when clicked.
XAML:
<CommandBar>
<CommandBar.SecondaryCommands>
<AppBarButton Label="Test" Click="AppBarButton_Click">
<AppBarButton.KeyboardAccelerators>
<KeyboardAccelerator Key="A" Modifiers="Control" />
</AppBarButton.KeyboardAccelerators>
</AppBarButton>
</CommandBar.SecondaryCommands>
</CommandBar>
Code behind:
private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("test clicked");
}
Am I missing something here or is this a bug in UWP?
Edit: I did some more testing and I'm fairly convinced that this is either a design flaw or a bug. If I make the button a primary command, the accelerator works while the button is visible, but if I resize the window so that it moves to the overflow menu, the accelerator stops working.
来源:https://stackoverflow.com/questions/53735503/keyboard-accelerator-stops-working-in-uwp-app