在MVVM模型中应该是模型实现INotifyPropertyChanged接口吗? [英] In MVVM model should the model implement INotifyPropertyChanged interface?
问题描述
我明确了解MVVM模式中的 View
和 ViewModel
。我正在计划在我的应用程序中实现MVVM模式。我正在面对这个模型的问题。我有.xml文件解析并显示在视图上的信息。
我需要首次通知模型中的更改。从需要开始,我需要收到通知。
那么如何实现模型?
我应该在模型类中实现 INotifyPropertyChanged
接口吗? (我看到该模型不应该实现 INotifyPropertyChanged
接口,因为它是WPF特定的)
在模型中实现 INotifyPropertyChanged
完全可以接受 -
通常,该模型实现了使
绑定到视图的便利。这通常意味着它支持属性和
收集更改的通知通过INotifyPropertyChanged
和
INotifyCollectionChanged
接口。代表
对象集合的模型类通常来源于
ObservableCollection< T>
类,它提供
INotifyCollectionChanged
界面。
尽管你决定是否要这种类型的实现与否,但请记住 -
如果您的模型类不实现所需的界面怎么办? p>
有时您将需要使用不
的模型对象来实现INotifyPropertyChanged
,INotifyCollectionChanged
,
IDataErrorInfo
或INotifyDataErrorInfo
接口。在这些情况下,视图模型可能需要包装模型对象并将
必需属性暴露给视图。这些属性的值将由
直接由模型对象提供。视图模型将
实现所暴露的属性所需的接口,因此
可以轻松地将数据绑定到它们。
取自 - http:// msdn。 microsoft.com/en-us/library/gg405484(PandP.40).aspx
我曾在一些项目中工作,我们尚未实施 INotifyPropertyChanged
在我们的模型中,由于这个我们面临很多问题;在虚拟机中需要不必要的重复属性,同时我们必须在将其传递给BL / DL之前更新底层对象(具有更新的值)。
如果您需要使用模型对象的收集(例如可编辑的网格或列表)或复杂模型,则特别面对问题;模型对象不会自动更新,您将不得不管理虚拟机中的所有对象。
I have clear idea about View
and ViewModel
in 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 parsed and showed the information on the View.
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 INotifyPropertyChanged
interface in model class also? (I read that model should not implement INotifyPropertyChanged
interface, since it is WPF specific)
Implementing INotifyPropertyChanged
in Models is totally acceptable -
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
andINotifyCollectionChanged
interfaces. Models classes that represent collections of objects typically derive from theObservableCollection<T>
class, which provides an implementation of theINotifyCollectionChanged
interface.
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
, orINotifyDataErrorInfo
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.
Taken from - http://msdn.microsoft.com/en-us/library/gg405484(PandP.40).aspx
I have worked in some projects where we haven't implemented INotifyPropertyChanged
in 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.
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.
这篇关于在MVVM模型中应该是模型实现INotifyPropertyChanged接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!