c# - 键盘事件未在 RT 应用程序中触发

标签 c# .net xaml windows-runtime

所以我一直在为 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
}

最佳答案

这样的事情怎么样:

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 .

关于c# - 键盘事件未在 RT 应用程序中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15913423/

相关文章:

wpf - Visual Studio 2012 XAML 设计器进程占用内存

C# - 是否有任何 DateTime.Now() 的字符串表示形式?

c# - OOP 对象创建指南

c# - iTextSharp - 移动 Acrofield

c# - 完全无法定义System.Runtime.Caching的UpdateCallback

C# xml序列化

xaml - Visual Studio 2015 Update 1 和 XAML 构建模板

xaml - 自定义 GroupItem HeaderTemplate UWP

c# - 独立于 DataTable 存储 DataRow - RowNotInTableException

c# - 英语国家的 Windows Phone 本地化