新的 ObservableCollection 与在循环中添加项目

发布于 2025-01-01 14:25:52 字数 512 浏览 0 评论 0原文

就速度和生成的通知数量而言,这段代码是否:

ObservableCollection<Foo> foo = new ObservableCollection<Foo>(bar);
this.SomeProperty = foo;

相同:

this.SomeProperty = new ObservableCollection<Foo>();

foreach (var v in bar) 
{
    this.SomeProperty.Add(v);
}

如果它们相同,是否可以以某种方式关闭生成的通知?

目标: 我正在尝试加快 Telerik RadChart 在 silverlight 中的显示速度。即使设置了包含 ObservableCollection 的属性,似乎也需要一段时间才能显示(并冻结浏览器应用程序)。绘制图表后,一切正常。

In terms of speed and the amount of notifications generated, is this code:

ObservableCollection<Foo> foo = new ObservableCollection<Foo>(bar);
this.SomeProperty = foo;

the same as:

this.SomeProperty = new ObservableCollection<Foo>();

foreach (var v in bar) 
{
    this.SomeProperty.Add(v);
}

If they are the same, is it possible to somehow turn off the notifications generated?

Objective:
I'm trying to speed up the display of Telerik RadChart in silverlight. It seems to take a while to display (and freezes the in browser app) even after the property containing ObservableCollection is set. Once the chart is drawn everything works correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春花秋月 2025-01-08 14:25:52
  1. 分析它或测试它!根据文档,
    添加、删除项目时,会发生 PropertyChanged 事件
    更改、移动或刷新整个列表。因此你可以
    编写一些仅订阅此事件的测试代码,看看会发生什么
    发生。

  2. 快速执行且线程安全的可观察集合 - 如果是由于持续不断的 OnChanged 事件,请考虑仅在批量更新后触发 - 有人已经为您完成了这项工作

  3. http://www.telerik.com/help/silverlight/ radchart-performance-tips-and-tricks.html
    专门处理您在页面一半处描述的场景。他们的结论与 2 相同 - 事实上,代码看起来非常相似:-)

如果冻结发生在绑定实际发生之前,那么我会确保延迟实际上不是基于渲染或由于其他原因活动(例如加载集合所花费的时间)。再说一次,分析是你的朋友。

  1. Profile it or Test it! According to the docs, the
    PropertyChanged event occcurs when an item is added, removed,
    changed, moved, or the entire list is refreshed. You can therefore
    write some test code that just subscribes to this event and see what
    happens.

  2. Fast performing and thread safe observable collection - if it is due to a constant barrage of OnChanged events, consider only firing after a bulk update – someone has done this work for you already

  3. http://www.telerik.com/help/silverlight/radchart-performance-tips-and-tricks.html
    deals specifically with the scenario you describe about half way down the page. Their conclusion is the same as 2 - in fact, the code looks very similar :-)

If the freeze is occuring before the binding actually takes place, then I'd make sure that the delay isn't actually rendering based or because of another actvity (like the time taken to load the collection). Again, profiling is your friend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文