WPF中的“禁用按钮"触发事件 [英] Disabled Button fires events in WPF

查看:54
本文介绍了WPF中的“禁用按钮"触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void but_Click(object sender, RoutedEventArgs e)
{
    (sender as Button).IsEnabled = false;
    doSomeThing();//e.g run for more than 20 seconds
    (sender as Button).IsEnabled = true;
}

当我第一次按下按钮时,它会禁用.然后启动 doSomeThing(),它包含UI代码或某些UI变量已更新.我的意思是,如果我在 doSomeThing()进行过程中再次按下该按钮,则在此按钮启用后,会再次触发 but_Click 事件.它维护事件触发的队列,即我按了n次.

When I press the button at first time it disables. Then starts doSomeThing() and it contains UI code or some UI variable updated. I mean while if I press the button again while doSomeThing() is in progress then but_Click event fires again after this button enables back. It maintains queue of event fired,i.e. n number of times which i pressed.

那么,如何在禁用按钮的情况下防止触发事件?请考虑在这种情况下,"doSomething"包含绑定到代码的UI控件.因此,在这种情况下,我们无法运行后台线程.帮助我解决问题.

So, how to prevent firing event while button is disabled? Please consider in this scenario 'doSomething' contains UI controls bind to code. So we can't run background Thread in this case. Help me with solution.

推荐答案

此代码的问题是UI线程中正在运行 doSomeThing()方法.因此,该按钮未正确禁用.如果我们重构代码,使 doSomeThing()方法在另一个线程中运行,它将很好地工作.这是一个使用 BackgroundWorker 的简单示例;但是我们的想法是我们不应该在UI线程中运行耗时的东西.这是重构的代码:

The issue with this code is doSomeThing() method is running in UI Thread. So, the Button is not properly disabled. If we refactor the code so that doSomeThing() method runs in a different thread, it will just work fine. Here is a simple example using BackgroundWorker; however the idea is we should not run time consuming stuffs in UI thread. Here is the refactored code:

public partial class ButtonEnableTest : Window
{
    private BackgroundWorker worker = new BackgroundWorker();

    public ButtonEnableTest()
    {
        InitializeComponent();
        this.worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
    }

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (this.btn.IsEnabled == false)
        {
            this.btn.IsEnabled = true;
        }
    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        doSomeThing();
    }

    private void doSomeThing()
    {
        int i = 5;
        while ( i > 0)
        {
            Thread.Sleep(TimeSpan.FromMilliseconds(2000));
            System.Diagnostics.Debug.WriteLine("Woke up " + i);
            i--;
        }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Button btn = (Button) sender;
        System.Diagnostics.Debug.WriteLine("at ButtonClick");
        if (btn.IsEnabled)
        {
            btn.IsEnabled = false;
            this.worker.RunWorkerAsync();
        }
    }
}

在这里我没有进行任何编码转换,因为我只是想分享我的想法.请注意,我将WPF按钮命名为"btn".

I did not follow any coding conversions here as I just wanted to share my idea. Please note that, I named the WPF button as "btn".

这篇关于WPF中的“禁用按钮"触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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