【问题标题】:KeyUp event is doubling using "mahapps.metro"KeyUp 事件使用“mahapps.metro”加倍
【发布时间】:2018-04-01 01:57:12
【问题描述】:

完成项目:https://uploadfiles.io/wz8ji

关注代码:

代码 C#:

public partial class MainWindow : MetroWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Textbox_KeyUp(object sender, KeyEventArgs e)
    {
        if (textbox.Text != string.Empty)
        {
            this.ShowMessageAsync("This is the title", "Some message");
        }
    }
}

代码 xaml:

<Controls:MetroWindow x:Class="Wpf.MainWindow"
        xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TabControl HorizontalAlignment="Left" Height="324" Margin="88,31,0,0" VerticalAlignment="Top" Width="605">
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5">
                    <TextBox 
                        x:Name="textbox"
                        HorizontalAlignment="Left"
                        Height="23"
                        Margin="10,10,0,0"
                        TextWrapping="Wrap"
                        Text="TextBox"
                        VerticalAlignment="Top"
                        Width="120"
                        KeyUp="Textbox_KeyUp"/>
                </Grid>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>
    </Grid>
</Controls:MetroWindow>

在我的文本框中,当我快速擦除并键入时,该事件被重复。当我正常删除并输入“1”时,一切都很好。问题是当我删除并输入任何非常快的字符时。当我快速执行此操作时,事件会重复。

我也用这个:http://mahapps.com/

我在没有使用“mahapps”的情况下创建了另一个项目,它工作正常。

事情是这样发生的:

按下退格键并快速松开+1键。

任何解决方案?

【问题讨论】:

  • 你能添加这个方法分配给的所有事件吗?
  • 嗯,让我试试你的代码
  • @PaulTsai 没有其他的了,仅此而已。
  • @MatheusMiranda 看起来您更新了代码以包含 xaml。
  • if 中的什么动作用于验证方法是否被调用?消息框?调试?

标签: c# wpf mahapps.metro


【解决方案1】:

嗯,我已经将你的代码提交给不同的测试并且方法没有重复......

int i = 0;
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if(((TextBox)sender).Text != string.Empty)
    {
        System.Diagnostics.Debug.WriteLine("Hello " + ++i);
    }
 }

确认另一个活动控件没有关联的键盘事件,这可能是问题所在。

我已经测试了你的代码,它运行良好

正如您在聊天中告诉我的那样,您快速按退格+ 1,嗯,错误是您的代码只要 TextBox 不为空,就检测到任何键的脉动,所以,您必须做的是限制返回的使用

private void Textbox_KeyUp(object sender, KeyEventArgs e)
{
    if (textbox.Text != string.Empty && e.Key != Key.Back)
     {
         this.ShowMessageAsync("This is the title", "Some message");
     }
}

【讨论】:

猜你喜欢
  • 2012-07-06
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2016-07-29
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多