为什么 UI 不更新(WPF)?

BJl*_*du4 1 c# wpf

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

<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Margin="345,258,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <TextBox x:Name="text" HorizontalAlignment="Left" Height="23" Margin="75,165,0,0" TextWrapping="Wrap" Text="{Binding Path=Count}" VerticalAlignment="Top" Width="311"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

和代码隐藏:

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++;                    
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

问题是什么。我写得正确吗?有更好的方法吗?

Fed*_*gui 6

为了支持双向 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));
        }));
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,让您MyClass继承此属性并在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.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)