.net 在 MVVM 模型中,模型应该实现 INotifyPropertyChanged 接口吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6922130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In MVVM model should the model implement INotifyPropertyChanged interface?
提问by Syed
I have clear idea about Viewand ViewModelin MVVM pattern. I am planning to implement MVVM pattern in my application. I'm facing an issue regarding the model. I have .xml file which is parsed and the information is displayed in the View.
我有清晰的思路View,并ViewModel在MVVM模式。我计划在我的应用程序中实现 MVVM 模式。我正面临有关模型的问题。我有解析的 .xml 文件,信息显示在视图中。
I need to be notified about the changes in the model for the first time only. From onwards on demand I need to be notified.
我只需要第一次收到有关模型更改的通知。从以后的需求,我需要得到通知。
So how to implement the model?
那么如何实现模型呢?
Should I implement INotifyPropertyChangedinterface in model class also? (I read that model should not implement INotifyPropertyChangedinterface, since it is WPF specific)
我INotifyPropertyChanged也应该在模型类中实现接口吗?(我读到模型不应该实现INotifyPropertyChanged接口,因为它是 WPF 特定的)
回答by akjoshi
Implementing INotifyPropertyChangedin Models is totally acceptable -
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
INotifyPropertyChangedandINotifyCollectionChangedinterfaces. Models classes that represent collections of objects typically derive from theObservableCollection<T>class, which provides an implementation of theINotifyCollectionChangedinterface.
通常,模型实现了易于绑定到视图的工具。这通常意味着它通过
INotifyPropertyChanged和INotifyCollectionChanged接口支持属性和集合更改通知。表示对象集合的模型类通常派生自ObservableCollection<T>提供INotifyCollectionChanged接口实现的 类 。
Although its up to you to decide whether you want that type of implementation or not, but remember -
虽然由您决定是否需要这种类型的实现,但请记住 -
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, orINotifyDataErrorInfointerfaces. 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.
如果您的模型类没有实现所需的接口怎么办?
有时候,你需要与不执行模型对象的工作
INotifyPropertyChanged,INotifyCollectionChanged,IDataErrorInfo,或INotifyDataErrorInfo接口。在这些情况下,视图模型可能需要包装模型对象并向视图公开所需的属性。这些属性的值将由模型对象直接提供。视图模型将为它公开的属性实现所需的接口,以便视图可以轻松地将数据绑定到它们。
Taken from - http://msdn.microsoft.com/en-us/library/gg405484(PandP.40).aspx
取自 - http://msdn.microsoft.com/en-us/library/gg405484(PandP.40).aspx
I have worked in some projects where we haven't implemented INotifyPropertyChangedin our models and due to this we faced a lot of issues; unnecessary duplication of properties was needed in VM and at the same time we had to update the underlying object(with updated values) before passing them to BL/DL.
我曾参与过一些我们没有INotifyPropertyChanged在模型中实现的项目,因此我们遇到了很多问题;VM 中需要不必要的属性重复,同时我们必须在将它们传递给 BL/DL 之前更新底层对象(具有更新的值)。
You will face problems specially if you need to work with collection of your model objects(say in an editable grid or list) or complex models; model objects won't be updated automatically and you will have to manage all that in your VM.
如果您需要处理模型对象的集合(例如在可编辑的网格或列表中)或复杂模型,您将面临特别的问题;模型对象不会自动更新,您必须在 VM 中管理所有这些。
回答by Pieter Müller
The standard MVVM approach is to implement INotifyPropertyChangedonly on the ViewModel. The purpose is to refresh the appropriate bindings on the View when something changes in the ViewModel.
标准的 MVVM 方法是INotifyPropertyChanged仅在 ViewModel 上实现。目的是在 ViewModel 发生变化时刷新 View 上的适当绑定。
However, this targets changes to the ViewModel by the View. That is to say, when you change the value in a TextBox, the INotifyPropertyChangedimplementation on the ViewModel will refresh the related Bindings, so the View updates correctly.
但是,这是针对View对 ViewModel的更改。也就是说,当你改变 a 中的值时TextBox,INotifyPropertyChangedViewModel 上的实现会刷新相关的 Bindings,从而使 View 正确更新。
It does notcover changes made to the Model by an external source, like Database changes or another interface. As long as all data modifications are coming from the View, the ViewModel should be aware of all changes and know what to update. For example, if you know that changing variable Fooon your Model will also change the value of Baron you Model, it would be wise to call both OnPropertyChanged(Foo)and OnPropertyChanged(Bar)in your ViewModel when you change the value of Foo.
它并没有覆盖由外部源对模型所做,像数据库的改变或另外的接口的改变。只要所有数据修改都来自 View,ViewModel 就应该知道所有更改并知道要更新什么。例如,如果你知道,更改变Foo你的模型也将发生变化的价值Bar你型号,这将是明智的,同时调用OnPropertyChanged(Foo)和OnPropertyChanged(Bar)当您更改的值在您的视图模型Foo。
The other alternative is to use events between the Model and the ViewModel to refresh those values on the ViewModel that require updating. If, as you say, the notification is required "first time only", then implementing a manual once off refresh on some trigger should also work.
另一种方法是使用 Model 和 ViewModel 之间的事件来刷新 ViewModel 上需要更新的那些值。如果,如您所说,“仅第一次”需要通知,那么在某些触发器上实施一次手动刷新也应该有效。
回答by Firoz
This is a very common problem while working with MVVM, INotifyPropertyChangedis not WPF specific as it is part of System.ComponentModelso no need to add any WPF specific reference in your solution.
这是使用 MVVM 时非常常见的问题,INotifyPropertyChanged它不是 WPF 特定的,因为它是其中的一部分,System.ComponentModel因此无需在您的解决方案中添加任何 WPF 特定参考。
If you will implement INofityPropertyChangedin your Model, it can save a lot more codes in ViewModel(Proxy Properties). So, it is acceptable your Model uses INotifyPropertyChanged.
如果你将INofityPropertyChanged在你的模型中实现,它可以在 ViewModel(代理属性)中节省更多的代码。因此,您的模型使用INotifyPropertyChanged.
回答by Klaus78
Sometimes it is acceptable to have the model implement the INotifyPropertyChangedinterface.
有时让模型实现INotifyPropertyChanged接口是可以接受的。
For example if the model has a lot of properties to be visualized and you want to avoid to implement a lot of code (proxy properties) in the viewmodel for exposing such model properties.
例如,如果模型有很多要可视化的属性,并且您想避免在视图模型中实现大量代码(代理属性)来暴露这些模型属性。
Look at http://msdn.microsoft.com/en-us/magazine/ff798279.aspx
回答by Mario Vernari
I'm not sure about what you mean. In the VM you may have either the INotifyPropertyChanged, or DependencyProperty-es (in this case the VM must derive from DependencyObject). Make no sense having both. Also doesn't make sense having none of them.
我不确定你的意思。在 VM 中,您可能拥有INotifyPropertyChanged或 DependencyProperty-es(在这种情况下,VM 必须从 派生DependencyObject)。两者兼得毫无意义。也没有任何意义。
In the model, you may do whatever you want. The ability to fire/receive events is good, but not always you can rely on them. Basically the model is depending on the source data and related stuffs, while the viewmodel has the load of interface the model with the presentation layer. Since WPF works upon events, at leastthe VM must provide some notification mechanism.
在模型中,您可以为所欲为。触发/接收事件的能力很好,但并非总是可以依赖它们。基本上模型取决于源数据和相关的东西,而视图模型具有模型与表示层的接口负载。由于 WPF 处理事件,至少VM 必须提供一些通知机制。
回答by Jimmy
This is a classic argument between the "pure" MVVM coders and others.
这是“纯”MVVM 编码器和其他人之间的经典争论。
I tend to go by the books whenever I can because most of the times that makes sense. However, in certain scenarios improvising the code according to the needs reduce a lot of duplicate code.
我倾向于尽可能按书本,因为大多数时候这是有道理的。但是,在某些场景下,根据需要即兴编写代码可以减少大量重复代码。
In your case, you can read the XML to a model class and either make a copy of the model class to the viewmodel, or copy the properties you want from the model to the view model. This way you have control for updating the UI/model. If you follow the first approach you need to have Inotifypropertychanged implemetned in your model class and it is acceptable.
在您的情况下,您可以将 XML 读取到模型类,然后将模型类复制到视图模型,或者将所需的属性从模型复制到视图模型。这样您就可以控制更新 UI/模型。如果您遵循第一种方法,则需要在模型类中实现 Inotifypropertychanged,这是可以接受的。
Having said that I would try my level best to follow the second approach because that would give me precise control over all the properties which is displayed/manipulated in the view. Also, I will feel much better that I am not breaking the MVVM pattern.
话虽如此,我会尽力遵循第二种方法,因为这将使我能够精确控制视图中显示/操作的所有属性。此外,我不会打破 MVVM 模式,我会感觉好多了。

