INotifyPropertyChanged 与 ViewModel 中的 DependencyProperty [英] INotifyPropertyChanged vs. DependencyProperty in ViewModel

查看:21
本文介绍了INotifyPropertyChanged 与 ViewModel 中的 DependencyProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Model-View-ViewModel 架构 WPF 应用程序中实现 ViewModel 时,似乎有两个主要选择如何使其可数据绑定.我已经看到将 DependencyProperty 用于视图将绑定的属性的实现,并且我已经看到 ViewModel 实现了 INotifyPropertyChanged.

When implementing the ViewModel in a Model-View-ViewModel architecture WPF application there seem to be two major choices how to make it databindable. I have seen implementations that use DependencyProperty for properties the View is going to bind against and I have seen the ViewModel implementing INotifyPropertyChanged instead.

我的问题是我什么时候应该更喜欢一个?是否有任何性能差异?将 ViewModel 依赖项赋予 WPF 真的是个好主意吗?在做出设计决策时,我还需要考虑什么?

My question is when should I prefer one over the other? Are there any performance differences? Is it really a good idea to give the ViewModel dependencies to WPF? What else do I need to consider when make the design decision?

推荐答案

Kent 写了一篇关于这个主题的有趣博客:查看模型:POCO 与 DependencyObjects.

Kent wrote an interesting blog about this topic: View Models: POCOs versus DependencyObjects.

简短摘要:

  1. 依赖对象未标记为可序列化
  2. DependencyObject 类覆盖并密封了 Equals() 和GetHashCode() 方法
  3. DependencyObject 具有线程关联性——它只能被访问在它所在的线程上已创建

我更喜欢 POCO 方法.可以在此处找到实现 INotifyPropertyChanged 接口的 PresentationModel(又名 ViewModel)的基类:http://compositeextensions.codeplex.com

I prefer the POCO approach. A base class for PresentationModel (aka ViewModel) which implements INotifyPropertyChanged interface can be found here: http://compositeextensions.codeplex.com

这篇关于INotifyPropertyChanged 与 ViewModel 中的 DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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