INotifyCollectionChanged——它多久触发一次(以及它们如何使其如此高效/快速)?
基本上,我想知道它在这里实际上是如何有效的。
示例代码:
void GetItems()
{
foreach (var item in items)
myObservableCollection.Add(item);
}
这不会每次都会触发 CollectionChanged 事件,导致 UI 每次都必须刷新吗?或者它是否这样做,以便等待直到 GetItems 函数完成?
基本上,WPF 似乎处理得很好,我想知道他们是如何做到的。
Basically, I'm wondering how it is actually efficient here.
Sample code:
void GetItems()
{
foreach (var item in items)
myObservableCollection.Add(item);
}
Won't this fire off the CollectionChanged event every time causing the UI to have to refresh everytime? Or does it do it so that it waits til the GetItems function is done?
Basically, it seems that WPF handles it very well, and I'm wondering how they did that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
优化性能:数据绑定提供了有关如何解析数据绑定的一些背景知识,包括不同项目来源的性能影响。请查看绑定到 ItemsSource 部分。
WPF 从不直接绑定到集合。如果将集合指定为绑定源,WPF 实际上会绑定到该集合的 默认值查看。
Optimizing Performance: Data Binding provides some background on how data bindings are resolved, including the performance implications of different items sources. Take a look at the Binding to an ItemsSource section.
WPF never binds directly to a collection. If you specify a collection as a binding source, WPF actually binds to the collection's default view.
每次更改都会触发该事件。
GUI 不必每次都做出反应和刷新,它可以推迟。
我知道WinForms会对此进行优化,我认为WPF也有类似的方法。
The event will fire for every change.
The GUI does not have to react and refresh every time, it can postpone that.
I know WinForms will optimize this, I think WPF has a similar approach.
如果您想查看 UI 请求新鲜结果的频率,请将其公开为公共属性,并将调试行放入 myObservableCollection 的公共属性的 get(评估器)中。
If you want to see how often the UI requests the fresh results expose it as a public property and put a debug line in the get (assessor) of the public property for myObservableCollection.