.net - 在 MVVM 模型中,模型应该实现 INotifyPropertyChanged 接口(interface)吗?

标签 .net wpf design-patterns mvvm inotifypropertychanged

我对 MVVM 模式中的 ViewViewModel 有清楚的了解。我计划在我的应用程序中实现 MVVM 模式。我面临有关模型的问题。我有 .xml 文件,该文件已被解析并且信息显示在 View 中。

我仅需要在第一次收到有关模型更改的通知。从需要时起,我需要收到通知。

那么如何实现该模型呢?

我还应该在模型类中实现 INotifyPropertyChanged 接口(interface)吗? (我读到该模型不应实现 INotifyPropertyChanged 接口(interface),因为它是 WPF 特定的)

最佳答案

实现INotifyPropertyChanged在模型中是完全可以接受的 -

Typically, the model implements the facilities that make it easy to bind to the view. This usually means it supports property and collection changed notification through the INotifyPropertyChanged and INotifyCollectionChanged interfaces. Models classes that represent collections of objects typically derive from the ObservableCollection<T> class, which provides an implementation of the INotifyCollectionChanged interface.

虽然由您决定是否需要这种类型的实现,但请记住 -

What if your model classes do not implement the required interfaces?

Sometimes you will need to work with model objects that do not implement the INotifyPropertyChanged, INotifyCollectionChanged, IDataErrorInfo, or INotifyDataErrorInfo interfaces. In those cases, the view model may need to wrap the model objects and expose the required properties to the view. The values for these properties will be provided directly by the model objects. The view model will implement the required interfaces for the properties it exposes so that the view can easily data bind to them.

取自 - http://msdn.microsoft.com/en-us/library/gg405484(PandP.40).aspx

我参与过一些我们尚未实现的项目 INotifyPropertyChanged在我们的模型中,因此我们遇到了很多问题; VM 中需要不必要的属性重复,同时我们必须在将其传递给 BL/DL 之前更新底层对象(使用更新的值)。

如果您需要处理模型对象的集合(例如在可编辑的网格或列表中)或复杂的模型,您将面临特别的问题;模型对象不会自动更新,您必须在虚拟机中管理所有这些。

关于.net - 在 MVVM 模型中,模型应该实现 INotifyPropertyChanged 接口(interface)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6922130/

相关文章:

c# - 如何在 .Net S3 SDK 中按名称获取存储桶?

c# - 升级到 SignalR-2.0.0-beta2 时未生成/signalr/hubs

wpf - 如何将关闭命令绑定(bind)到按钮

javascript - 使用 JavaScript/Node.js 实现插件架构

c++ - 内存分配器范围的设计

c# - 如何使用 C# 在 switch-case 条件下使用当前 case 语句的值跳转到另一个 case 语句

c# - 使用单一方法返回不同类型的数据

c# - 任务的返回类型错误

c# - 名称 "Settings"在命名空间 "clr-namespace:MyApp.Properties"中不存在

c# - Entity Framework 存储库模式继承