为什么UI不更新(WPF)?

我正在尝试WPF绑定。 我写了小应用程序,但有问题,我的UI没有更新。 这是我的代码:

 

代码隐藏:

 namespace WpfApplication1 { public partial class MainWindow : Window { MyClass mc; public MainWindow() { InitializeComponent(); mc = new MyClass(this.Dispatcher); text.DataContext = mc; } private void Button_Click(object sender, RoutedEventArgs e) { Task task = new Task(() => { mc.StartCounting(); }); task.ContinueWith((previousTask) => { }, TaskScheduler.FromCurrentSynchronizationContext()); task.Start(); } } public class MyClass { public int Count { get; set; } public Dispatcher MainWindowDispatcher; public MyClass(Dispatcher mainWindowDispatcher) { MainWindowDispatcher = mainWindowDispatcher; } public void StartCounting() { while (Count != 3) { MainWindowDispatcher.Invoke(() => { Count++; }); } } } 

}

问题是什么。 我是否正确地写了这个,有没有更好的方法来做到这一点?

为了支持双向WPF DataBinding,您的数据类必须实现INotifyPropertyChanged接口 。

首先,创建一个可以通过将它们编组到UI线程来通知属性更改的类:

 public class PropertyChangedBase:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { Application.Current.Dispatcher.BeginInvoke((Action) (() => { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); })); } } 

然后,让MyClass从此inheritance并在Count属性更改时正确引发属性更改通知:

 public class MyClass: PropertyChangedBase { private int _count; public int Count { get { return _count; } set { _count = value; OnPropertyChanged("Count"); //This is important!!!! } } public void StartCounting() { while (Count != 3) { Count++; //No need to marshall this operation to the UI thread. Only the property change notification is required to run on the Dispatcher thread. } } }