c# - 我应该在 MVVM 的模型中使用 ObservableCollections 吗?

标签 c# wpf mvvm observablecollection

我正在将 MVVM 用于代表模型并允许用户与之交互的 WPF 客户端。我一直避免在实际模型中使用 ObservableCollection 类(而是在该模型中选择通用集合,如 IList,然后在基础集合发生变化时将该 IList 转换为 ViewModel 上的实际数据绑定(bind) ObservableCollection)。原因是 MSDN 将该类呈现为 WPF 和以 UI 为中心:

You can enumerate over any collection that implements the IEnumerable interface. However, to set up dynamic bindings so that insertions or deletions in the collection update the UI automatically, the collection must implement the INotifyCollectionChanged interface. This interface exposes the CollectionChanged event, an event that should be raised whenever the underlying collection changes. WPF provides the ObservableCollection class, which is a built-in implementation of a data collection that implements the INotifyCollectionChanged interface.

问题:我的区分真的有必要吗?这是额外的工作和额外的代码。我知道这个主题对于 SO 来说可能过于模糊和主观,但也许每个人都遵循明确的、普遍认可的约定。

最佳答案

是的,我认为你做得对。可观察集合属于表示层。将它们添加到您的 View 模型,而不是您的领域模型。

This question may be of some help

but maybe there are clear, universally agreed-upon conventions everybody follows.

找到后请告诉我,谢谢。

关于c# - 我应该在 MVVM 的模型中使用 ObservableCollections 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42463886/

相关文章:

mvvm - 我应该在哪里为 SwiftUI 中的 View 创建模型实例?

c# - 如何 "finalize"新行

c# - 如何用字符串值定义枚举?

c# - 获取 NUnit 断言失败消息

c# - 如何检测 Windows 7 开始菜单何时打开

mvvm - WinRT中的UI逻辑-MVVM

c# - DbSet 表名

c# - 我如何从 Brush 继承,以便我可以添加一个 Name 属性?

C# wpf 列表框未从 ObservableCollection 更新

c# - ViewModel 在 MVVM Light 中没有收到消息