c# - 使用 "mahapps.metro"KeyUp 事件加倍

标签 c# wpf mahapps.metro

完整项目: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键。

enter image description here

有什么解决办法吗?

最佳答案

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

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

Verify that another active control has no associated keyboard event, that may be the problem.

Your code works correctly

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

正如您在聊天中告诉我的,您快速按退格键 + 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");
     }
}

关于c# - 使用 "mahapps.metro"KeyUp 事件加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49594114/

相关文章:

c# - BeginInvoke 方法的全部内容,以便它不会关闭我的应用程序?

C# DataGridView 自动添加一_Extra_行

c# - 在 C# WPF 中创建可绑定(bind)点

c# - 绑定(bind)到 WPF 中的数组

wpf - MahApps 和 Catel MVVM

c# - 与 MahAppsMetro ProgressIndicator 绑定(bind)失败

javascript - ASP.NET URL 编码不带 ?或变量名

c# - 使用 .NET 的 Process 类重定向标准输入/输出/错误流

c# - 什么是最佳选择,Windows 窗体或 C# 中的 WPF 开发?

c# - 如何设置 MahApps 窗口网格的样式?