为什么UI元素在Button Click事件处理程序中反映了他们的状态? [英] Why don't UI elements reflect their state in Button Click event handler?

查看:115
本文介绍了为什么UI元素在Button Click事件处理程序中反映了他们的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下示例中,如何获取:

In the following example, how can I get:


  • 按钮为disabled-gray

  • 说工作...的消息

/ strong>正在完成工作,而不是 之后的 完成工作?

while the work is being done, not after the work is done?

XAML:

<Window x:Class="TestIsEnabled8938.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel Margin="10" HorizontalAlignment="Left">

        <Button x:Name="Button_Refresh" 
                    HorizontalAlignment="Left"  
                    DockPanel.Dock="Top" 
                    Content="Refresh" 
                    Click="Button_Refresh_Click" 
                    Height="25" 
                    Width="200"/>

        <TextBlock x:Name="Message" Text="Button is ready to click."/>
    </StackPanel>
</Window>

代码隐藏

using System.Windows;
using System.Threading;

namespace TestIsEnabled8938
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Refresh_Click(object sender, RoutedEventArgs e)
        {
            Message.Text = "working...";
            Button_Refresh.IsEnabled = false;

            //do work
            Thread.Sleep(2000);

            Message.Text = "Button is ready to click again.";
            Button_Refresh.IsEnabled = true;
        }
    }
}





This doesn't work either:

Dispatcher.Invoke(new Action(() => { Message.Text = "working..."; }));
Dispatcher.Invoke(new Action(() => { Button_Refresh.IsEnabled = false; }));



答案:



感谢Heinzi,代码作品:

Answer:

Thanks Heinzi, this code works:

using System.Windows;
using System.Threading;
using System.ComponentModel;

namespace TestIsEnabled8938
{
    public partial class Window1 : Window
    {
        BackgroundWorker backgroundWorker;

        public Window1()
        {
            InitializeComponent();

            backgroundWorker = new BackgroundWorker();

            backgroundWorker.DoWork += (sender, args) =>
            {
                Thread.Sleep(3000);
            };

            backgroundWorker.RunWorkerCompleted += (sender, args) =>
            {
                Message.Text = "button is ready to click again";
                Button_Refresh.IsEnabled = true;
            };
        }

        private void Button_Refresh_Click(object sender, RoutedEventArgs e)
        {
            Message.Text = "working...";
            Button_Refresh.IsEnabled = false;
            backgroundWorker.RunWorkerAsync();
        }
    }
}


推荐答案

如果您希望UI在任务运行时更新(并保持响应),则需要使用单独的线程,例如使用 BackgroundWorker

If you want the UI to update (and stay responsive) while your task is running, you need to use a separate thread, for example by using a BackgroundWorker.

代码示例(未测试):

BackgroundWorker bwButtonWorker;

public Window1() {
    InitializeComponent();

    bwButtonWorker = new BackgroundWorker();

    bwButtonWorker.DoWork += (sender, args) => {
        // do your lengthy stuff here -- this happens in a separate thread
        Thread.Sleep(2000);
    }

    bwButtonWorker.RunWorkerCompleted += (sender, args) => {
        // this happens in the UI thread, so you can modify your UI elements here
        Message.Text = "Button is ready to click again.";
        Button_Refresh.IsEnabled = true;
    }
}

private void Button_Refresh_Click(object sender, RoutedEventArgs e)
{
    Message.Text = "working...";
    Button_Refresh.IsEnabled = false;
    bwButtonWorker.RunWorkerAsync();
}

这篇关于为什么UI元素在Button Click事件处理程序中反映了他们的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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