是 ObservableCollection适用于非UI场景

发布于 2024-12-04 06:17:02 字数 250 浏览 3 评论 0原文

我对 ObservableCollection 不太熟悉,但实现它似乎为我提供了一种基于任何添加/删除/替换/清除操作更新自定义集合状态的便捷方法。

但是,我查看的示例通常在 WPF/WinForms 数据绑定上下文中引用它。

实施它是否会带来我应该警惕的任何不需要的开销/依赖性,即。在我的场景中(简单地更新我根据添加/删除等添加到集合中的扩展状态)是否最好只实现 Collection 或类似的并通过覆盖每个添加/删除/替换/清除操作来更新扩展状态?

I'm not very familiar with ObservableCollection but implementing it seems to provide me with a convenient way of updating a custom collections state based on any add/remove/replace/clear operation.

However, the examples I've looked at typically reference it in the context of WPF/WinForms data binding.

Does implementing it bring any unwanted overhead/dependencies that I should be wary of, ie. in my scenario (simply updating extended state which I have added to the collection based on adds/removes etc.) would it be better to just implement Collection or similar and update the extended state by overriding each add/remove/replace/clear operation?

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

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

发布评论

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

评论(3

掀纱窥君容 2024-12-11 06:17:02

ObservableCollection 通常与 UI 无关的原因是,如果您使用 MVVM 模式,它在更新视图方面提供了灵活性。话虽如此,我认为没有理由阻止您在非 UI 代码中使用它。请参阅 1 了解性能相关信息。

The reason ObservableCollection is more often than not associated with UI is because of the flexibility it provides in updating the view if you are using MVVM pattern. Having said that I see no reason it should prevent you from using it in non-UI code. Please refer 1 for perfromance related information.

半寸时光 2024-12-11 06:17:02

如果您想在 WPF / WinForms 应用程序之外使用该集合,则对该集合没有很强的依赖性。它只是实现了一个方便的接口。

There is no strong dependency with that collection if you want to use it outside of a WPF / WinForms application. It just implements a convenient interface.

吲‖鸣 2024-12-11 06:17:02

即使在 WPF 3.0 版本的框架中引入了 ObservableCollection,但它与 UI 概念没有很强的联系。另外,如果您阅读 MSDN 文档,类型的描述并不以任何方式引用 UI 上下文:

表示动态数据集合,在添加、删除项目或刷新整个列表时提供通知。

事实上,它非常适合 UI 场景,因为它已经实现了 INotifiyPropertyChanged。所以回答你的问题,是的,在非 UI 场景中使用 ObservableCollection 是完全可以的。

Even if the ObservableCollection has been introduced in the framework in version 3.0 which also introduced WPF, it has no strong link with UI concepts. Also if you read the MSDN documentation, the description of the type does not refer to UI context in any way:

Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

The fact is that it is just well suited for UI scenarios as it already implements INotifiyPropertyChanged. So to reply your question, yes, it is totally ok using ObservableCollection in non-UI scenarios.

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