gpt4 book ai didi

c# - 使用 return/enter 而不是 tab 会跳过 UWP 应用程序中的元素

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:20 25 4
gpt4 key购买 nike

我希望能够使用 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 上放置一个断点。当您按下任意键时,处理程序将执行一次。但是当你按下 Enter 时,它实际上连续执行了两次,这导致你的焦点跳了两行。

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

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

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

关于c# - 使用 return/enter 而不是 tab 会跳过 UWP 应用程序中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49131887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com