【问题标题】:Keyboard events not firing in RT appRT 应用程序中未触发的键盘事件
【发布时间】:2013-04-09 22:05:11
【问题描述】:

所以我一直在为 Windows 应用商店开发一个宠物项目,并且遇到了一个绊脚石:键盘事件拒绝触发。我尝试将焦点强制到主页上,尝试将焦点强制到网格上,但似乎没有任何帮助。有人遇到过这样的问题吗?我的 google-fu 让我失望了。

相关代码:

XAML:

<Page
    x:Class="PlatformTD.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PlatformTD"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
          Name="LayoutRoot">
        <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                Name="MainCanvas" x:FieldModifier="public"
                SizeChanged="MainCanvas_SizeChanged"
                Loaded="MainCanvas_Loaded" />
    </Grid>
</Page>

C#:

public MainPage()
{
    this.InitializeComponent();
    MainCanvas.KeyDown += MainPage_KeyDown;
    MainCanvas.KeyUp += MainPage_KeyUp;
    KeyDown += MainPage_KeyDown;
    KeyUp += MainPage_KeyUp;
    LayoutRoot.KeyDown += MainPage_KeyDown;
    LayoutRoot.KeyUp += MainPage_KeyUp;
}
private void MainPage_KeyUp(object sender, KeyRoutedEventArgs e)
{ // breakpoint here to catch when event fires
    // does stuff
}
private void MainPage_KeyDown(object sender, KeyRoutedEventArgs e)
{ // breakpoint here to catch when event fires
    // does stuff
}

【问题讨论】:

  • 这些键盘事件仅在您的控件具有焦点时引发。如果您想处理所有关键事件 - 使用 Igor 的建议从核心窗口获取它们。否则,您需要启用对控件的关注并赋予它们焦点。

标签: c# .net xaml windows-runtime


【解决方案1】:

这样的事情怎么样:

public MainPage()
{
    this.InitializeComponent();
    Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
    Window.Current.CoreWindow.KeyUp += CoreWindow_KeyUp;
}

void CoreWindow_KeyUp(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
    throw new NotImplementedException();
}

void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
    throw new NotImplementedException();
}

在我身边工作。解决方案found here

【讨论】:

    猜你喜欢
    • 2016-08-30
    • 1970-01-01
    • 2011-03-06
    • 2015-08-21
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    相关资源
    最近更新 更多