简体   繁体   English

如何在页面加载时处理UWP按下事件?

[英]How to handle a UWP keydown event on page load?

I can start typing in my control after clicking within the Grid, but I would like to be able to start typing without having to click first. 在网格内单击后,我可以开始在控件中键入内容,但是我希望能够不必先单击就可以开始键入内容。 Is there something that I can put in the code behind to give the keyboard input focus on page load? 我可以在代码后面添加一些内容来使键盘输入专注于页面加载吗?

In my View I have: 在我看来,我有:

     <Grid x:Name="PageGrid"
      KeyDown="{x:Bind ViewModel.OnKeyboardNumberInput, Mode=OneWay}"

In my ViewModel I have: 在我的ViewModel中,我有:

        public void OnKeyboardNumberInput(object sender, KeyRoutedEventArgs e)
    {

You can do it in the Loaded event of your Page , this event 您可以在PageLoaded事件中执行此操作

Occurs when a FrameworkElement has been constructed and added to the object tree, and is ready for interaction. 在构造FrameworkElement并将其添加到对象树并准备进行交互时发生。

And if you want to enable the keyboard input, your control should be editable. 而且,如果要启用键盘输入,则控件应该是可编辑的。

When you navigate to a Page , it will automatically focus on the first control in this Page . 当您导航到Page ,它将自动专注于该Page的第一个控件。 But we can change this focus like this: 但是我们可以这样改变焦点:

XAML: XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBox x:Name="txt1" VerticalAlignment="Center" HorizontalAlignment="Center" Width="400" />
    <TextBox x:Name="txt2" VerticalAlignment="Bottom" HorizontalAlignment="Center" Width="400" />
</Grid>

code behind: 后面的代码:

public MainPage()
{
    this.InitializeComponent();
    this.Loaded += Page_Loaded;
}

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    txt2.Focus(FocusState.Keyboard);
}

Noticed that you may have used MVVM pattern for your project, you can do it like this: 注意,您可能已经在项目中使用了MVVM模式,您可以这样做:

XAML: XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Loaded="{x:Bind ViewModel.page_Loaded, Mode=OneWay}">
    <TextBox x:Name="txt1" VerticalAlignment="Center" HorizontalAlignment="Center" Width="400" />
    <TextBox x:Name="txt2" VerticalAlignment="Bottom" HorizontalAlignment="Center" Width="400" />
</Grid>

code behind in your ViewModel: 您的ViewModel中的代码如下:

public void page_Loaded(object sender, RoutedEventArgs e)
{
    var grid = sender as Grid;
    var tb = (TextBox)grid.FindName("txt2");
    tb.Focus(FocusState.Keyboard);
}

If you want to type in a specific control, setting focus to that control is better than attempting to handle keyboard events. 如果要键入特定的控件,则将焦点设置为该控件比尝试处理键盘事件更好。 Assuming a simple window: 假设一个简单的窗口:

<Window
    x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Loaded="OnWindowLoaded"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox x:Name="myTextBox" />
    </Grid>
</Window>

And in code-behind: 并在代码背后:

private void OnWindowLoaded(object sender, RoutedEventArgs e)
{
    myTextBox.Focus();
}

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

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