是 ObservableCollection适用于非UI场景
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
如果您想在 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.
即使在 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:
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.