MVVM - 模型或视图模型中的 PropertyChanged? [英] MVVM - PropertyChanged in Model or ViewModel?

查看:29
本文介绍了MVVM - 模型或视图模型中的 PropertyChanged?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些 MVVM 教程,我已经看到这两种方式都完成了.大多数使用 ViewModel for PropertyChanged(这是我一直在做的),但我在模型中遇到了一个这样做的.两种方法都可以接受吗?如果是这样,不同方法的优点/缺点是什么?

I have gone through a few MVVM tutorials and I have seen this done both ways. Most use the ViewModel for PropertyChanged (which is what I have been doing), but I came across one that did this in the Model. Are both methods acceptable? If so, what are the benefits/drawbacks of the different methods?

推荐答案

INotifyPropertyChanged (INPC) 接口用于Binding.

The INotifyPropertyChanged (INPC) interface is used for Binding.

因此,在一般情况下,您希望在 ViewModel 中实现它.

So, in the average case, you want to implement it in your ViewModel.

ViewModel 用于将 Model 与您的 View 解耦,因此您的 Model 中不需要 INPC,因为您不希望 Bindings 到您的 Model.

The ViewModel is used to decouple the Model from your View, so there is no need to have INPC in your Model, as you do not want Bindings to your Model.

在大多数情况下,即使对于较小的属性,您仍然拥有一个非常小的 ViewModel.

In most cases, even for smaller properties, you still have a very small ViewModel.

如果您想要 MVVM 的坚实基础,您可能会使用某种 MVVM 框架,例如 caliburn.微.使用它会给你一个 ViewModelBase(或这里的 NotifyPropertyChangedBase),这样你就不必自己实现这些接口成员,只需使用 NotifyOfPropertyChange(() => MyProperty),这样更简单,更不容易出错.

If you want a solid base for MVVM, you are probably going to use some kind of MVVM Framework like caliburn.micro. Using it will give you a ViewModelBase (or here NotifyPropertyChangedBase) so that you do not have to implement those interface members yourself and can just use NotifyOfPropertyChange(() => MyProperty), which is way easier and less error prone.

更新由于似乎有很多 Windows 窗体开发人员,这里有一个很好的这篇文章将更深入地了解 MVVM 是关于什么的:MVVM 上的 MSDN 杂志

UPDATE As there seem to be many Windows Forms developers out there, here is an excellent article that will give deeper understanding of what MVVM is about: MSDN Magazine on MVVM

我特别链接了问题所在的数据模型部分.

I have linked especially the part about the datamodel, that the question is about.

这篇关于MVVM - 模型或视图模型中的 PropertyChanged?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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