在MVVM模式应该模型实现INotifyPropertyChanged接口? [英] In MVVM model should the model implement INotifyPropertyChanged interface?

查看:256
本文介绍了在MVVM模式应该模型实现INotifyPropertyChanged接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于查看视图模型在MVVM模式清晰的概念。我计划实现MVVM模式在我的应用程序。我现在面临有关模型的问题。我有一个解析,并显示在浏览信息的.xml文件。

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?

我应该实现模型类也 INotifyPropertyChanged的界面? (我读的模式不应该实施 INotifyPropertyChanged的接口,因为它是WPF特定的)

Should I implement INotifyPropertyChanged interface in model class also? (I read that model should not implement INotifyPropertyChanged interface, since it is WPF specific)

推荐答案

实施 INotifyPropertyChanged的的模式是完全可以接受的 -

Implementing INotifyPropertyChanged in Models is totally acceptable -

通常情况下,模型实现,可以很容易的设施   绑定到视图。这通常意味着它支持财产和   收集通过改变通知 INotifyPropertyChanged的和    INotifyCollectionChanged 接口。即重新present模型类   对象的集合通常从派生   的ObservableCollection< T> 类,它提供了一个实现    INotifyCollectionChanged 接口。

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.

虽然它由你来决定你是否需要该类型的实现与否,但要记住 -

Although its up to you to decide whether you want that type of implementation or not, but remember -

如果你的模型类不实现所需的接口?

有时候,你需要与不模型对象工作   贯彻落实 INotifyPropertyChanged的 INotifyCollectionChanged ,    IDataErrorInfo INotifyDataErrorInfo 接口。在这些情况下,   视图模型可能需要换行的模型对象,并揭露   要求的性能的图。这些属性值将   直接由模型对象提供。将视图模型   实现所需要的接口,它暴露了这样的属性   该视图可以方便地将数据绑定到他们。

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 /库/ gg405484(PandP.40)的.aspx

我已经在我们还没有实现 INotifyPropertyChanged的我们的模型,由于这一点,我们遇到了很多问题,一些项目的工作;需要在虚拟机,并​​在同一时间,我们不得不将它们传递到BL / DL之前更新底层对象(与更新后的值)的属性不必要的重复。

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆