我有一个简单的页面,上面有一个按钮和一个标签。当我点击按钮我想要标签文本更新时,我已经复制代码hereWPF上的UpdateLayout点击
但是,当我使用下面的示例。直到功能完成后文本才会更新。有任何想法吗。 WPF窗体窗体使用按钮和标签放在上面。
我在按钮的代码隐藏下面有以下内容,第一条消息从不显示。
Thread.Sleep意味着从数据库中提取数据返回到屏幕,这可能需要3到30秒的时间,因此为什么我想让它显示用户的东西。
任何想法???
using System.Threading;
using System.Windows;
using System.Windows.Threading;
namespace NetSpot.RESV4.Presentation
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
label1.Content = "Button 1 Clicked";
ForceUIToUpdate();
Thread.Sleep(4000);
label1.Content = "button 1 Updated";
}
public static void ForceUIToUpdate()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, new DespatcherOperationCallback(delegate(object parameter)
{
frame.Continue = false;
return null;
}), null);
Dispatcher.PushFrame(frame);
}
}
}
是否有特别的理由不使用绑定属性,INotifyPropertyChanged的?如果不是,那看起来不如调度员复杂。 – ianschol 2012-03-01 15:54:23