什么时候不应该使用 ObservableCollection 和数据绑定?

发布于 2024-08-19 04:11:06 字数 201 浏览 10 评论 0原文

dhopton 的 评论让我思考。在哪些情况下您不想使用 ObservableCollection?

dhopton's comment got me thinking. What are some situations where you wouldn't want to use an ObservableCollection?

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

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

发布评论

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

评论(1

韶华倾负 2024-08-26 04:11:06

当您想要更好地控制 ObservableCollection 发送的通知时。示例:默认实现虽然支持添加元素范围,但只是对整个集合进行重置(我相信),而不是逐个抛出包含所有新项目的单个通知。部分原因是 WPF 中的默认 CollectionView 不支持尺寸 > 的 INotifyCollectionChanged 通知。 1(如果你这样做它就会抛出),所以它没有太多动力去这样做。但是,如果您使用的是第 3 方网格,例如 Xceed,它具有 CollectionView 派生,确实支持任意通知大小。这意味着当您将数据捆绑到列表中时,您可以通过将通知分组来获得一些相当不错的性能提升。

请注意,有一些与通知大小和通知优先级相关的有趣拐点,这些拐点会影响感知性能和实际性能(两者都不是您想象的那样)。

这是为什么您希望接口松散的一个主要示例:我们输入为 IList,并将实现更改为我们自己的异步加载集合,该集合在加载数据块时触发集合更改事件。 View 不必知道这种差异。它就是有效的。

When you want to have greater control over the notifications being sent by ObservableCollection. Example: The default implementation, while it supports adding ranges of elements, just throws a reset (I believe) for the whole collection rather than piece meal throwing out a single notification with all the new items on it. Part of this is because the default CollectionView in WPF doesn't support INotifyCollectionChanged notifications with Sizes > 1 (it throws if you do), so theres not much motivation for it to. However, if you are using a 3rd party grid, such as Xceed, it has a CollectionView derivation that does support arbitrary notification sizes. This means when you're bundling data into a list, you can get some pretty nice performance bumps by having the notifications grouped up.

Note that there are some interesting inflection points related to notification size, and priority of notifications that impact perceived performance, and actual performance (both not how you'd think).

It's a primary example of why you want your interface to be loose: We type as IList, and changed our implementation to our own, async-loading collection that fires collection changed events as we load chunks of data. View doesn't have to know about this difference. It just works.

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