1

关于ObservableCollection的更新与不更新分析 - cbaa

 1 year ago
source link: https://www.cnblogs.com/cbaa/p/17378609.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

关于ObservableCollection的更新与不更新分析

因为最近在WPF项目中,遇到ObservableCollection这个属性的频繁使用,一个一个坑跳过来,今天看到这个贴子

玩转INotifyPropertyChanged和ObservableCollection - 包建强 - 博客园 (cnblogs.com)

其中分析很透彻了,但是留了一点遗憾,而且在其中引起了一个想法,做一个项目来测试一下。

我们知道在Binding一个item的时候,想要其中属性变化时,UI同步变化,需要实现OnPropertyChanged接口,我因为习惯于mvvm.Toolkit,也就是说需要继承ObservableObject,

类似这样:

 public class Student:ObservableObject
    {
        private string name;
        public string Name
        {
            get => name;
            set => SetProperty(ref name, value);
        }
        private int age;
        public int Age
        {
            get => age;
            set=>SetProperty(ref age, value);
        }
}

在Binding到集合项的时候,通常需要这样

pulic  ObservableCollection<Student> Students_t = new ();

通过上面2条,可以实现:集合中增加、删除元素,及元素属性更新的时候,UI可自动更新。

但是,但是的但是,如果需要刷新数据的时候,比如说,人工清除当前集合,重新从源头读取,通常这样:

Students=new ObservableCollection<Student>(ctx.students)

这就坏了,发现UI没更新。原因在于,ObservableCollection只关注内部的元素变化,但当他自己发生了变化,其实对集合Binding关系已经破坏了,因为这是一个新建的集合,等于更新了Students这个对象,而不是对这个对象的内部元素作出的增删。

希望我说得明白了。

如果我说明白了,就是说,前面我们忽略了一个问题。

Students仍然不是可观察对象。

所以,要修改为

 private ObservableCollection<Student> students_c = new ();
public ObservableCollection<Student> Student_c
        {
            get => students_c;
            set => SetProperty(ref students_c, value);
        }
}

开始我说的忽发奇想是,如果我对List<T>实现可观察,是否能够代替了ObservableCollection?测试结果是,是的,在重置数据的时候,确实是可以实现的(很奇怪吗?),但是在增删元素的时候,仍然不行,仍然需要ObservableCollection。

对以上各种情况,我做的测试项目,见:

https://gitee.com/ppcba/observable-collection-demo.git

如果有同样疑惑的,建议参考着自己做一下,不然会仍然糊涂着。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK