【问题标题】:Detect a Key stroke in WinRT App在 WinRT 应用程序中检测击键
【发布时间】:2012-10-01 11:16:19
【问题描述】:

我有一个包含 Canvas 和几个按钮的基本页面模板。画布上有一些动画正在进行。我也想检测这个页面上的击键。

我在 LayoutRoot Grid 上使用了 KeyDown 事件,它以前可以工作,但随着我进一步构建应用程序,它停止工作。我也尝试在 PageRoot 中使用 KeyDown 事件,但仍然无法正常工作。

我正在粘贴下面的 XAML。

<common:LayoutAwarePage
x:Name="pageRoot"
x:Class="Shooter.Game"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
IsTabStop="false"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Shooter"
xmlns:common="using:Shooter.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" >

<Page.Resources>

    <!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
    <x:String x:Key="AppName">Hits</x:String>
</Page.Resources>

<!--
    This grid acts as a root panel for the page that defines two rows:
    * Row 0 contains the back button and page title
    * Row 1 contains the rest of the page layout
-->
<Grid Style="{StaticResource LayoutRootStyle}" Background="Navy" KeyDown="Grid_KeyDown_1" >
    <Grid.RowDefinitions>
        <RowDefinition Height="95"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="150" />
    </Grid.RowDefinitions>

    <!-- Back button and page title -->
    <Grid Name="navBar">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="backButton" Grid.Column="0" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
        <TextBlock Visibility="Collapsed" x:Name="pageTitle" Grid.Column="1" Text="{StaticResource AppName}" Style="{StaticResource PageHeaderTextStyle}"/>
        <TextBlock x:Name="hitCountTxt" Grid.Column="1" Text="Hits: 0" Style="{StaticResource PageHeaderTextStyle}" />
        <TextBlock x:Name="missCountTxt" Grid.Column="1" Text="Miss: 0" Style="{StaticResource PageHeaderTextStyle}" HorizontalAlignment="Right" />
    </Grid>
    <Grid Grid.Row="1" Name="gameBoard">
        <Canvas Name="warZone" Width="Auto" />
    </Grid>
    <Grid Grid.Row="2" Name="dashBoard">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Button Name="Dit" Grid.Column="0" Content="Dit" Width="200" Height="140" FontSize="40" Click="Dit_Click" />
        <Button Name="Dah" Grid.Column="2" Content="Dah" Width="200" Height="140" FontSize="40" Click="Dah_Click" />
        <TextBlock Name="fireCodeTxt" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" Text="No shots Fired!" FontSize="60" />
    </Grid>

    <VisualStateManager.VisualStateGroups>

        <!-- Visual states reflect the application's view state -->
        <VisualStateGroup x:Name="ApplicationViewStates">
            <VisualState x:Name="FullScreenLandscape"/>
            <VisualState x:Name="Filled"/>

            <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
            <VisualState x:Name="FullScreenPortrait">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>

            <!-- The back button and title have different styles when snapped -->
            <VisualState x:Name="Snapped">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</Grid>

【问题讨论】:

    标签: windows xaml windows-runtime keydown


    【解决方案1】:

    您可以在 NavigatedTo 事件中订阅 Window.Current.CoreWindow.KeyDown/Up 并在 NavigatedFrom 中取消订阅。这些应该有效。否则,具有焦点的控件将有机会处理这些事件并将它们标记为已处理以防止它们冒泡。

    【讨论】:

      【解决方案2】:

      您可以在页面构建时订阅按键向上/向下事件。见以下代码:

          public MyPage()
          {
              InitializeComponent();            
              KeyUp += MyPage_KeyUp;           
          }
      
          private void MyPage_KeyUp(object sender, KeyRoutedEventArgs e)
          {
              //Your code here
          }        
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-28
        • 2018-02-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多