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

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

问题描述

我对 MVVM 模式中的 ViewViewModel 有了清晰的认识.我计划在我的应用程序中实现 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 is parsed and the information is displayed in 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.

那么如何实现模型呢?

我是否也应该在模型类中实现 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)

推荐答案

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

Implementing INotifyPropertyChanged in Models is totally acceptable -

通常,该模型实现了易于绑定到视图.这通常意味着它支持财产和通过 INotifyPropertyChangedINotifyCollectionChanged 接口.代表的模型类对象的集合通常派生自ObservableCollection 类,它提供了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 -

如果您的模型类没有实现所需的接口怎么办?

有时您需要使用不实现 INotifyPropertyChangedINotifyCollectionChangedIDataErrorInfoINotifyDataErrorInfo 接口.在这些情况下,视图模型可能需要包装模型对象并公开视图所需的属性.这些属性的值将由模型对象直接提供.视图模型将为它公开的属性实现所需的接口视图可以轻松地将数据绑定到它们.

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 之前更新底层对象(具有更新的值).

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.

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

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天全站免登陆