在WPF中,我可以使用InputBindings属性很容易地将键盘快捷方式绑定到窗口级别的方法。在WinRT中有什么等价物呢?在WinRT中将键盘快捷方式绑定到方法的正确方法是什么?
在WPF中,我可以使用InputBindings属性很容易地将键盘快捷方式绑定到窗口级别的方法。在WinRT中有什么等价物呢?在WinRT中将键盘快捷方式绑定到方法的正确方法是什么?
访问键是您的应用程序中UI的快捷方式。访问键由Alt键加字母键组成。
加速键是应用程序命令的快捷方式。您的应用程序可能与该命令完全对应的UI,也可能没有。加速键由Ctrl键加字母键组成。
以下示例演示了媒体播放、暂停和停止按钮的可访问实现的快捷键:
<MediaElement x:Name="Movie" Source="sample.wmv"
AutoPlay="False" Width="320" Height="240"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="Play" Margin="1,2"
ToolTipService.ToolTip="shortcut key: Ctrl+P"
AutomationProperties.AccessKey="Control P">
<TextBlock><Underline>P</Underline>lay</TextBlock>
</Button>
<Button x:Name="Pause" Margin="1,2"
ToolTipService.ToolTip="shortcut key: Ctrl+A"
AutomationProperties.AccessKey="Control A">
<TextBlock>P<Underline>a</Underline>use</TextBlock>
</Button>
<Button x:Name="Stop" Margin="1,2"
ToolTipService.ToolTip="shortcut key: Ctrl+S"
AutomationProperties.AccessKey="Control S">
<TextBlock><Underline>S</Underline>top</TextBlock>
</Button>
</StackPanel>
<object AutomationProperties.AcceleratorKey="ALT+F" />
重要提示!设置AutomationProperties.AcceleratorKey或AutomationProperties.AccessKey并不会启用键盘功能。它只是向UI自动化框架报告应使用哪些键,以便通过辅助技术将此信息传递给用户。键处理的实现仍然需要在代码中完成,而不是在XAML中。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Set the input focus to ensure that keyboard events are raised.
this.Loaded += delegate { this.Focus(FocusState.Programmatic); };
}
private void Grid_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Control) isCtrlKeyPressed = false;
}
private void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Control) isCtrlKeyPressed = true;
else if (isCtrlKeyPressed)
{
switch (e.Key)
{
case VirtualKey.P: DemoMovie.Play(); break;
case VirtualKey.A: DemoMovie.Pause(); break;
case VirtualKey.S: DemoMovie.Stop(); break;
}
}
}