问题描述
对于是否应该在模型中实现 INotifyPropertyChanged
似乎存在相互矛盾的想法.我认为它应该在 ViewModel 中实现,但我无法弄清楚它是如何实现的.在 stackoverlow.com 上有很多地方提到了同样的想法(在 MVVM 模型中,模型应该实现 INotifyPropertyChanged 接口吗?, 在 MVVM 中,ViewModel 或 Model 应该实现 INotifyPropertyChanged 吗?),但我找不到任何示例来说明如何做到这一点.
There seems to be conflicting thoughts on whether INotifyPropertyChanged
should be implemented in the Model or not. I think that it should be implemented in the ViewModel, but I can't figure out how it would be accomplished. There are plenty of mentions of this same idea all over stackoverlow.com ( In MVVM model should the model implement INotifyPropertyChanged interface?, In MVVM should the ViewModel or Model implement INotifyPropertyChanged?), but I can't find any example to show how to do it.
假设例如我有一个模型人:
Let's say for example I have a model Person:
Public Person {
public int Age { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public void NameChange( string newName );
}
我将如何实现 ViewModel 以便识别 Age
、FirstName
或 LastName
中的更改?
How would I implement the ViewModel so that changes in Age
, FirstName
, or LastName
are all recognized?
Public PersonViewModel : INotifyPropertyChanged {
Person _person;
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName) {
if(this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
//ctor, Properties, etc...
}
编辑 - 说明:
那么在不更改 Person 模型的情况下如何修改 ViewModel 以获取更新通知?
So without changing the Person model how do I modify the ViewModel to get notified of the updates?
这可能吗?如果不是,那些订阅模型中的 INPC 是 baaaad"的人如何收到模型更改的通知?
Is that even possible? If not, how are those that subscribe to the "INPC in the model is baaaad" get notified of changes in the model?
推荐答案
有趣的问题.我已经阅读 MVVM 一年多了,但我仍然不确定.
Interesting question. I've read about MVVM for more than a year now, and I'm still not sure about it.
例如,如果您的应用程序表示一个进程的状态,并且该状态在没有用户任何交互的情况下在内部进行了修改,那么您的模型需要能够通知您的视图模型它已更改.因此,如果您的模型实现了 INotifyPropertyChanged,并且您的视图模型只将相同的信息传递给视图,那么……您的视图模型真的需要存在吗……?
If your application is representing a state of a process for example, and this state is modified internally without any interaction of the user, then your model needs to be able to notify your viewmodel that it changed.So if your model implement INotifyPropertyChanged, and your viewmodel only pass the same informations to the view, then... does your viewmodel really need to exist...?
在我们公司,我们主要考虑两种情况:
In our company, we consider two main cases:
我们在开发之前使用非常严格的 UML 分析来构建我们的软件(不是那么敏捷).当我们想要在屏幕上显示我们的对象时,它们返回给我们不同的视图,这些视图在需要时与 Bindings 一起使用(使用 ContentControl 左右).我们软件所需的大多数视图都显示这些类型的对象,它们实现了 INotifyPropertyChanged,因此也是一种 ViewModel.
We structure our software with a quite strict UML analysis before developping (not so agile). When we then want to display our objects on screen, they return us their different views, which are used when needed with Bindings (using ContentControl or so). Most of the views we need for our software display these kinds of object, that implement INotifyPropertyChanged and are therefore also kind of ViewModels.
为了构建软件主视图(视图结构),我们为它们创建全局视图和视图模型.那是我们真正遵循 MVVM 实践的时候.
To build the software main Views (view structure), we create global views and ViewModels for them. That's when we really follow the MVVM practices.
也许我错过了关于 MVVM 的一点,但根据我的经验,这不是您绝对必须始终遵循的模式.开发 WPF 应用程序是一种非常好的思维方式,但为每个视图创建 ViewModel 在我看来是一个很大的开销.
Maybe I missed a point about MVVM, but in my experience, it's not a pattern that you absolutely have to always follow. It's a very good way of thinking to develop WPF applications, but creating ViewModels for each and every view seems to me like a big overhead.
大家觉得这种做法怎么样?
What do all of you think of this way of doing?
最好的问候,
安东尼
编辑 31.03.2012
我发现了一篇非常有趣的文章,解释了如何在 viewmodel 中处理模型属性,而无需在 viewModel 中为每个属性实现代理属性.作者还说了一些关于在模型中实现 INPC 以及监听它的视图模型的话.我认为这是迄今为止我读过的关于 MVVM 的最实用的文章.一探究竟 :http://msdn.microsoft.com/en-us/magazine/ff798279.aspx
I have found a very interesting article explaining how to handle your model properties in the viewmodel, without having to implement a proxy property in the viewModel for each one of them.Also the writer say some words about having INPC implemented in the model, and the viewmodel listening to it.I think this is the most practical oriented article I've read about MVVM so far.Check it out :http://msdn.microsoft.com/en-us/magazine/ff798279.aspx
这篇关于当模型中的属性发生变化时收到通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!