使用return / enter而不是tab跳过UWP app中的元素

时间:2018-03-06 13:22:22

标签: c# xaml uwp uwp-xaml

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

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

FocusManager.TryMoveFocus(FocusNavigationDirection.Next)

它从第一个元素跳到第三个元素到第五个元素。选项卡工作正常,不会跳过任何元素。

这是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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

要查看发生了什么,请在事件处理程序中的if上添加断点。按任意键时,处理程序执行一次。但是当你按 Enter 时,它实际上会连续执行两次,这会使你的焦点跳到两行。

修复非常简单 - 只需将事件标记为已处理:

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

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