gpt4 book ai didi

c# - ObservableCollection 数据绑定(bind)性能

转载 作者:太空狗 更新时间:2023-10-29 22:56:18 25 4
gpt4 key购买 nike

我想知道为什么要根据这个article与 WPF 中的 List<> 集合相比,可观察集合的绑定(bind)速度明显更快(20 毫秒对 1685 毫秒,快 800 倍)。我查看了 ObservableCollection 的内部结构,它使用一个 List 作为它的存储集合对象(我使用了反射器并在构造函数中看到了这个)

public Collection()
{
this.items = new List<T>();
}

那么这里发生了什么?

最佳答案

那篇文章中的比较不是两个简单的绑定(bind)操作之间的比较,这些测量指的是将单个项目添加到 WPF 的场景 ListBox已经绑定(bind)到 List<T>ObservableCollection<T> .

正如作者所说:

...the CLR List<T> object does not automatically raise a collection changed event. In order to get the ListBox to pick up the changes, you would have to recreate your list of employees and re-attach it to the ItemsSource property of the ListBox. While this solution works, it introduces a huge performance impact. Each time you reassign the ItemsSource of ListBox to a new object, the ListBox first throws away its previous items and regenerates its entire list.

这解释了性能差异。尽管ObservableCollection<T>List<T> 支持, 它实现了 INotifyCollectionChanged接口(interface),它使所有额外的处理都变得不必要。

关于c# - ObservableCollection 数据绑定(bind)性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1007691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com