[UWP]如果焦点在TextBox上,CoreWindow.CharacterReceived将触发两次 [英] [UWP] CoreWindow.CharacterReceived fires twice if the focus is on a TextBox

查看:41
本文介绍了[UWP]如果焦点在TextBox上,CoreWindow.CharacterReceived将触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个说明问题的示例:

Here's an example to illustrate the problem:

显示按下的键盘按键的键代码的应用程序。在MainPage.xaml中有一个只读的TextBox:

An app that displays key codes of pressed keyboard buttons. In MainPage.xaml there's a read-only TextBox:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel>
            <TextBox Name="KeyCodesTextBox" IsReadOnly="True"/>
        </StackPanel>
    </Grid>

代码隐藏:

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

            Window.Current.CoreWindow.CharacterReceived += CoreWindow_CharacterRecieved;
        }
        private void CoreWindow_CharacterRecieved(object sender, CharacterReceivedEventArgs e)
        {
            KeyCodesTextBox.Text += e.KeyCode.ToString() + " ";
        }
    }

应用程序工作正常,但用户在TextBox具有焦点时按键的情况除外 - 然后CoreWindow_CharacterRecieved触发两次将2个项目添加到按下的密钥代码历史记录中。

The app works fine, except for the situation when user presses a key while the TextBox has the focus - then CoreWindow_CharacterRecieved fires twice thus adding 2 items to the pressed-key-code history.

想知道如何解决问题。

既不添加e.Handled = true;也不是(e.KeyStatus.RepeatCount!= 1)返回;没有帮助。

Neither adding e.Handled = true; nor if(e.KeyStatus.RepeatCount != 1) return; doesn't help.

推荐答案

Hello DeliciousSteak,

Hello DeliciousSteak,

我看到你已经为TextBox设置了"IsReadOnly"。如果您只想显示关键代码,可以使用TextBlock而不是textbox。我测试了TextBlock。它运行良好。

I saw that you’ve set "IsReadOnly" as true for TextBox. If you just want to show key code, you could use TextBlock instead of textbox. I’ve tested TextBlock. It worked well.

否则,如果您只想使用TextBox,则可以将"IsEnable"设置为false。然后它也会起作用。 

Else, if you just only want to use TextBox, you could set "IsEnable" as false. Then it also will work. 

最好的问候,

Xavier Eoro

Xavier Eoro


这篇关于[UWP]如果焦点在TextBox上,CoreWindow.CharacterReceived将触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆