使用了Thread.Sleep时不是WPF标签更新() [英] Wpf Label not being updated when using Thread.Sleep()

查看:137
本文介绍了使用了Thread.Sleep时不是WPF标签更新()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标签,我想以后每一秒更新,3秒后我只看到最后一个字符串第三步......我在做什么错误,有另一种方式,如果实现这一目标的内容出于某种原因,我不能使用Thread.sleep()方法:



查看:

 <窗​​口x:类=WpfApplication1.ScrollerView
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
标题=滚轮的DataContext ={StaticResource的scrollerVM}HEIGHT =150WIDTH =300> ;
<网格和GT;
< ListBox中的ItemsSource ={结合消息}WIDTH =200HEIGHT =50了borderThickness =0VerticalAlignment =顶部的Horizo​​ntalAlignment =左>
< ItemsControl.ItemTemplate>
<&DataTemplate的GT;
<标签内容={结合文字}/>
< / DataTemplate中>
< /ItemsControl.ItemTemplate>
< /列表框>
<按钮宽度=70HEIGHT =24CONTENT =添加新的命令={结合的AddNew}的Horizo​​ntalAlignment =左保证金=0,56,0,30/>
< /网格和GT;
< /窗GT;

查看模式:



 使用System.Collections.ObjectModel; 
使用System.ComponentModel;
使用的System.Threading;

命名空间WpfApplication1.Scroller
{
公共类消息
{
公共信息(字符串_text)
{
文本= _文本;
}

私人字符串文本;
公共字符串文本
{
{返回文本; }
组的{text =值;}
}
}

公共类ScrollerViewModel:INotifyPropertyChanged的
{
公共事件PropertyChangedEventHandler的PropertyChanged;
公共DelegateCommand的AddNew {搞定;保护套; }

&的ObservableCollection LT;消息> _messages =新的ObservableCollection<消息>();
公众的ObservableCollection<消息>消息
{
{返回_messages; }

{
_messages =价值;
OnPropertyChanged(信息);
}
}

公共ScrollerViewModel()
{
的AddNew =新DelegateCommand(添加);
}

私人无效添加(对象参数)
{
的UpdateProgress(第1步......);
的UpdateProgress(第2步......);
的UpdateProgress(第三步......);
}

私人无效的UpdateProgress(字符串步)
{
Messages.Clear();
Messages.Add(新消息(步骤));
Thread.sleep代码(1000);
}

保护无效OnPropertyChanged(字符串propertyName的)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(propertyName的));
}
}
}
}


解决方案

这是因为你在UI线程睡觉。该UI将不会有机会更新,直到添加完成。您可以使用一个BackgroundWorker与ReportProgress达到你想要的东西。事情是这样的:

  BackgroundWorker的工人=新的BackgroundWorker(); 
worker.WorkerReportsProgress = TRUE;
worker.DoWork + =委托(对象S,DoWorkEventArgs参数)
{
worker.ReportProgress(1,第一步);
Thread.sleep代码(1000);
worker.ReportProgress(2,步骤2);
Thread.sleep代码(1000);
worker.ReportProgress(3,第三步);
};
worker.ProgressChanged + =委托(对象S,ProgressChangedEventArgs参数)
{
串步=(字符串)args.UserState;
Messages.Clear();
Messages.Add(新消息(步骤));
};
worker.RunWorkerAsync();



UI线程不会,而的DoWork 被执行,但在 ProgressChanged 将在UI线程上执行。


I have a Label the Content of which I would like to update after each second,after 3 seconds I only see the last string "Step 3..." What am I doing wrong and is there another way to achieve this if for some reason I cannot use Thread.Sleep():

View:

<Window x:Class="WpfApplication1.ScrollerView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Scroller" DataContext="{StaticResource scrollerVM}" Height="150" Width="300">
    <Grid>
        <ListBox ItemsSource="{Binding Messages}" Width="200" Height="50" BorderThickness="0" VerticalAlignment="Top" HorizontalAlignment="Left">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding Text}"  />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>
        <Button Width="70" Height="24" Content="Add new" Command="{Binding AddNew}" HorizontalAlignment="Left" Margin="0,56,0,30" />
    </Grid>
</Window>

View model:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;

namespace WpfApplication1.Scroller
{
    public class Message
    {
        public Message(string _text)
        {
            text = _text;
        }

        private string text;
        public string Text
        {
            get { return text; }
            set {text = value;}
        }
    }

    public class ScrollerViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public DelegateCommand AddNew { get; protected set; }

        ObservableCollection<Message> _messages = new ObservableCollection<Message>();
        public ObservableCollection<Message> Messages
        {
            get { return _messages; }
            set
            {
                _messages = value;
                OnPropertyChanged("Messages");
            }
        }

        public ScrollerViewModel()
        {
            AddNew = new DelegateCommand(Add);
        }

        private void Add(object parameter)
        {
            UpdateProgress("Step 1...");
            UpdateProgress("Step 2...");
            UpdateProgress("Step 3...");
        }

        private void UpdateProgress(string step)
        {
            Messages.Clear();
            Messages.Add(new Message(step));
            Thread.Sleep(1000);
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

解决方案

It is because you are sleeping in the UI thread. The UI won't have a chance to update until Add is finished. You can use a BackgroundWorker with ReportProgress to achieve what you want. Something like this:

BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
    worker.ReportProgress(1, "Step1");
    Thread.Sleep(1000);
    worker.ReportProgress(2, "Step2");
    Thread.Sleep(1000);
    worker.ReportProgress(3, "Step3");
};
worker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
    string step = (string)args.UserState;
    Messages.Clear();
    Messages.Add(new Message(step));
};
worker.RunWorkerAsync();

The UI thread won't be occupied while DoWork is executed, but the code in ProgressChanged will be performed on the UI thread.

这篇关于使用了Thread.Sleep时不是WPF标签更新()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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