赫雷卡!
我遇到了同样的问题,但使用的是 Windows 8.1。我花了 2 天时间才找到解决方案。这是愚蠢的 SwapChainPanel,它不能成为焦点,因为该类没有从 Control 类继承,所以它不会处理键盘事件。
解决方案在这里,也就是说,您必须放置一个从 Control 类继承的 XAML 元素,例如 Button 来处理事件。我的 XAML 文件是这样的:
<SwapChainPanel x:Name="_swapChainPanel"
Loaded="_swapChainPanel_Loaded"
KeyDown="_swapChainPanel_KeyDown">
<Button x:Name="_swapChainButton"
Content="Button"
HorizontalAlignment="Left"
Height="0"
VerticalAlignment="Top"
Width="0"
KeyDown="_swapChainButton_KeyDown">
</Button>
</SwapChainPanel>
在 XAML.cs 中,我以这种方式处理事件:
private void _swapChainButton_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
e.Handled = false; //This will pass the event to its parent, which is the _swapChainPanel
}
private void _swapChainPanel_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
game.KeyboardEvent();
}
在 KeyboardEvent() 方法中,我放置了 if things... 你必须使用代码手动使按钮成为焦点。"_swapChainButton.Focus(FocusState.Programmatic);"
但最后,它对我来说不是那么好,它太慢了。它有延迟。:/