简体   繁体   English

KeyDown 事件不会从 UWP 中的选定网格触发

[英]KeyDown event doesn't trigger from selected grid in UWP

I'm working on custom control like calendar, where I have a panel as month view in which Grid cells are arranged as Date cells.我正在研究像日历这样的自定义控件,其中我有一个面板作为月视图,其中网格单元格排列为日期单元格。 The grid has a content presenter with TextBlock as a content template.网格有一个以 TextBlock 作为内容模板的内容展示器。 Everything working fine.一切正常。 Pointerpressed, pointerReleased and OnTapped override methods are working fine for Grid but OnKeyDown override method or KeyDown event doesn't trigger from it. Pointerpressed、pointerReleased 和 OnTapped 覆盖方法对 Grid 工作正常,但 OnKeyDown 覆盖方法或 KeyDown 事件不会从中触发。 I also tried with PreviewKeyDown.我也试过 PreviewKeyDown。 Please help me to overcome this issue.请帮助我克服这个问题。

KeyDown event doesn't trigger from selected grid in UWP KeyDown 事件不会从 UWP 中的选定网格触发

The problem is that the Grid was not focused, so the event will not be triggered as expect.问题是 Grid 没有聚焦,因此不会按预期触发事件。 The better way is that listen CoreWindow KeyDown event, because when the app is running in foreground, CoreWindow will be focused automatically.更好的方法是监听CoreWindow KeyDown事件,因为当应用程序在前台运行时,CoreWindow 会自动聚焦。

Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
   MyBlock.Text = args.VirtualKey.ToString();
}

The key down event only trigger from keyboard focused control.按键按下事件仅从键盘聚焦控制触发。 And the Grid is not a control and the Grid is Panel.并且 Grid 不是控件,Grid 是 Panel。 In UWP, only the control can set focus.在 UWP 中,只有控件可以设置焦点。

You can write a empty control and add it to Grid to set the control focus.您可以编写一个空控件并将其添加到 Grid 以设置控件焦点。

class Foo : Control
{
    protected override void OnKeyDown(KeyRoutedEventArgs e)
    {
        Debug.WriteLine("Foo key down");
    }
}

    <Grid x:Name="Grid2" Margin="10,10,10,10" Width="100" Background="#565656" HorizontalAlignment="Right" 
          KeyDown="Grid2_OnKeyDown">
        <local:Foo x:Name="Foo"></local:Foo>
    </Grid>

You can write any code to help debug when the Grid2 key down.当 Grid2 键按下时,您可以编写任何代码来帮助调试。

    private void Grid2_OnKeyDown(object sender, KeyRoutedEventArgs e)
    {
        Debug.WriteLine("Grid2 key down");
    }

And then you can set the Foo property focus when the Grid2 clicked.然后您可以在单击 Grid2 时设置 Foo 属性焦点。

    private async void Grid2_OnPointerReleased(object sender, PointerRoutedEventArgs e)
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Foo.Focus(FocusState.Keyboard); });
    }

Try to run the code and you can see the output windows show the message when you click the Grid2 and press key.尝试运行代码,您可以看到 output windows 在您单击 Grid2 并按下 键时显示该消息。

But why I write the code in dispatcher, because I should make the Foo control get focus.但是为什么我在调度程序中编写代码,因为我应该让 Foo 控件获得焦点。 And if I do not use dispatcher and the pointer focus will in Grid and not any UIElement focus keyboard.如果我不使用调度程序并且指针焦点将在 Grid 中,而不是任何 UIElement 焦点键盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM