使用 return/enter 而不是制表符会跳过 UWP 应用程序中的元素

Using return/enter instead of tab skips an element in UWP app

我希望能够使用 return/enter 进入 UWP 应用程序中的下一个 TextBox。 (我在这里包含了一个示例应用程序,但我的实际应用程序有很多行,所以如果可以避免的话,我不想为每个 TextBox 添加一个 KeyDown 事件。 )

我已将 KeyDown="Grid_KeyDown" 添加到我的 Grid 然后,如果用户按 Enter,我将调用

FocusManager.TryMoveFocus(FocusNavigationDirection.Next)

它从第一个元素跳到第三个元素到第五个。 Tab 在不跳过任何元素的情况下工作正常。

这里是XAML:

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

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" KeyDown="Grid_KeyDown">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition  Height="Auto"></RowDefinition>
            <RowDefinition  Height="Auto"></RowDefinition>
            <RowDefinition  Height="Auto"></RowDefinition>
            <RowDefinition  Height="Auto"></RowDefinition>
            <RowDefinition  Height="Auto"></RowDefinition>
            <RowDefinition  Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>

        <TextBlock Grid.Column="0" Grid.Row="0">First:</TextBlock>
        <TextBlock Grid.Column="0" Grid.Row="1">Second:</TextBlock>
        <TextBlock Grid.Column="0" Grid.Row="2">Third:</TextBlock>
        <TextBlock Grid.Column="0" Grid.Row="3">Fourth:</TextBlock>
        <TextBlock Grid.Column="0" Grid.Row="4">Fifth:</TextBlock>
        <TextBlock Grid.Column="0" Grid.Row="5">Sixth:</TextBlock>


        <TextBox Margin="10" Grid.Column="1" Grid.Row="0"  TabIndex="1" ></TextBox>
        <TextBox Margin="10" Grid.Column="1" Grid.Row="1"  TabIndex="2" ></TextBox>
        <TextBox Margin="10" Grid.Column="1" Grid.Row="2"  TabIndex="3" ></TextBox>
        <TextBox Margin="10" Grid.Column="1" Grid.Row="3"  TabIndex="4" ></TextBox>
        <TextBox Margin="10" Grid.Column="1" Grid.Row="4"  TabIndex="5" ></TextBox>
        <TextBox Margin="10" Grid.Column="1" Grid.Row="5"  TabIndex="6" ></TextBox>

    </Grid>
</Page>

下面是代码:

using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace CatApp
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
            }
        }
    }
}

这实际上是自 Windows 8 以来一直存在于 WinRT TextBox 中的一个已知错误,它似乎已在 Fall Creators Update (16299)。如果您的应用针对此版本的 Windows(或更高版本),它应该可以正常运行。

要查看发生了什么,请在事件处理程序的 if 上放置一个断点。当您按下任意键时,处理程序将执行一次。但是当你按下回车的时候其实是连续执行了两次,导致你的焦点跳了两行

修复非常简单 - 将事件标记为已处理就足够了:

private void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Enter)
    {
        FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
        e.Handled = true;
    }
}

这将防止处理程序执行两次,您的应用程序将按预期工作:-)。