WPF Binding INotifyPropertyChanged 다중 스레드 이해

7596 단어 property
4
  • 먼저 하나의 예를 보겠습니다.
    Person.cs
    public class Person : ObservableObject,INotifyPropertyChanged
    
        {
    
            private string _testName;
    
            private ObservableCollection<string> _names=new ObservableCollection<string>();
    
    
    
            public string TestName
    
            {
    
                get
    
                {
    
                    return _testName;
    
                }
    
                set
    
                {
    
                    _testName = value;
    
                    OnPropertyChanged();
    
                }
    
            }
    
    
    
            public ObservableCollection<string> Names
    
            {
    
                get { return _names; }
    
                set
    
                {
    
                    _names = value;
    
                    RaisePropertyChanged(()=>Names);
    
                }
    
            }
    
    
    
            public event PropertyChangedEventHandler PropertyChanged;
    
    
    
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    
            {
    
                PropertyChangedEventHandler handler = PropertyChanged;
    
                if (handler != null)
    
                {
    
                    handler(this, new PropertyChangedEventArgs(propertyName));
    
                }
    
            }
    
        }

    MainWindow.xaml.cs
    public partial class MainWindow : Window, INotifyPropertyChanged
    
        {
    
            private int _currentId;
    
            private Person _person;
    
    
    
            public Person Person
    
            {
    
                get
    
                {
    
                    return _person;
    
                }
    
                set
    
                {
    
                    _person = value;
    
                    OnPropertyChanged();
    
                }
    
            }
    
    
    
            public MainWindow()
    
            {
    
                InitializeComponent();
    
    
    
                this.DataContext = this;
    
    
    
                Person = new Person();
    
    
    
                Person.TestName = "TestName";
    
                Person.Names.Add("string");
    
    
    
                _currentId = Thread.CurrentThread.ManagedThreadId;
    
    
    
                DispatcherHelper.Initialize();
    
            }
    
    
    
            public event PropertyChangedEventHandler PropertyChanged;
    
    
    
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    
            {
    
                PropertyChangedEventHandler handler = PropertyChanged;
    
                if (handler != null)
    
                {
    
                    handler(this, new PropertyChangedEventArgs(propertyName));
    
                }
    
            }
    
    
    
            private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    
            {
    
                Task.Factory.StartNew(() =>
    
                {
    
                    if (_currentId == Thread.CurrentThread.ManagedThreadId)
    
                    {
    
                        Person = new Person();
    
                    }
    
                    else
    
                    {
    
                        //
    
                        Person.TestName = "NotSame";
    
    
    
                        //
    
                        Person.Names.Add("Hello");
    
                    }
    
                });
    
    
    
    
    
            }
    
    
    
        }

    주석에 주의하다
    결과적으로TestName 속성은 UI에 올바르게 업데이트될 수 있지만 집합 속성Names는 그렇지 않습니다.
    나머지 의 이해 는 한 편 은 잘 썼다
    http://www.cnblogs.com/wpcockroach/p/3909081.html

    좋은 웹페이지 즐겨찾기