zoukankan      html  css  js  c++  java
  • WPF Binding INotifyPropertyChanged 多线程 深入理解

    • 例子

    先来看一个例子

    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

  • 相关阅读:
    C#中IEnumerable、ICollection、IList、List之间的区别
    H5中画图标签Canvas---画矩形,画线,画圆,渐变色,图形载入
    centos启用ftp功能
    CentOS 7 安装FTP服务器(vsftpd)
    Linux下如何修改用户默认目录
    Centos搭建FTP服务器
    MyBatis 示例之存储过程
    MyBatis:MyBatis操作MySQL存储过程
    mybatis的select、insert、update、delete语句
    日常运维中的相关日志切割处理方法总结 [Logrotate、python、shell脚本实现 ]
  • 原文地址:https://www.cnblogs.com/HelloMyWorld/p/4287971.html
Copyright © 2011-2022 走看看