MVVM 模型应该是什么样的?
您好,我有 3 个关于 MVVM 模型的问题。
- 有没有办法绕过冗余的
PropertyChanged("PropName");
- 将 POCO 对象包装到 WPF 的最佳方法是什么
INotifyPropertyChanged, IDataErrorInfo
- 我应该如何与(WPfWrapers - POCO)在 ViewModel 内 - 通过强制转换或属性...
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里有 3 个答案:
您可以找到引发 PropertyChanged 事件的替代方案,而无需在 .NET 社区。然而,它们都有其他缺点(例如性能)。
最好的方法是直接在模型中实现 INotifyPropertyChanged 和 IDataErrorInfo。这并不总是可能的。如果您需要包装模型类,那么您可能会查看 DataModel 概念。
我不确定我是否理解了最后一个问题,但这是一个答案。 ViewModel 或 DataModel 应直接与模型交互。但这些类不应该以直接的方式与视图交互。对于此类场景,请使用接口(例如 IView)。
更多信息可在此处找到:WPF 应用程序框架 (WAF)
Here are 3 answers:
You find alternatives of raising the PropertyChanged event without passing the “PropName” as string parameter in the .NET community. However, all of them have other drawbacks (e.g. performance).
The best way is to implement INotifyPropertyChanged and IDataErrorInfo directly in the Model. That’s not always possible. If you need to wrap you Model classes then you might have a look at the DataModel concept.
I’m not sure if I understand the last question right but here is an answer. The ViewModel or DataModel should interact with the Model directly. But these classes shouldn’t interact with the View in a direct way. Use interfaces (e.g. IView) for such scenarios.
More information can be found here: WPF Application Framework (WAF)
是的,您可以使用 Lamdba 表达式来完成此操作。但这会花费一些处理器时间(进行一些快速测量:这种方法比使用字符串常量慢大约 200 倍。在频繁使用的 POCO 上使用表达式时请记住这一点):
我认为您不这样做必须包装它们(除了创建相应的视图模型之外)。 POCO 用作模型,接口由视图模型实现。
Yes, you can do this with a Lamdba expression. But this will cost some processor time (made some quick measurements: this approach is around 200 times slower than using the the string constant. Keep this in mind when using the expression on highly frequented POCOs):
I think you don't have to wrap them (besides creating a coresponding view model). The POCOs are used as model and the interfaces are implemented by the viewmodel.
关于你的第一个问题:
看看这篇文章
并且< a href="http://www.google.nl/search?q=INotifyPropertyChanged%20implementations" rel="nofollow">进行 google 搜索
有很多方法(和讨论)
我的2分钱:
首先
还有第二个
On your first question:
Have a look at this post
And do a google search
There are many ways (and discussions)
My 2 cents:
First
And second
还可以选择在视图模型中使用依赖项属性。很多人似乎不喜欢这样做,因为它们是 wpf 的一部分并且具有线程关联性(您只能从创建该特定对象的线程中调用依赖属性方法,
我个人从未发现这是一个问题,因为您' re view 既依赖于 wpf 并且具有线程亲和性,因此即使使用 INotifyPropertyChanged,您仍然必须从正确的线程触发 PropertyChanged 事件
Dependecy 属性已内置通知支持,并且不需要。 wpf 进行任何反射,因此它们的数据绑定速度更快(但设置/获取速度较慢,尽管时间范围很短)
您的场景可能与我的不同,但我认为它值得一看:)
There is also the option to use Dependency properties in your viewmodel. Alot of people doesnt seem to like doing this because they are a part of wpf and have thread affinity (you can only call the dependency property method from the thread that created that perticular object
I personaly have never found this to be a problem since you're view is both dependent on wpf and has thread affinity anyway, so you even if INotifyPropertyChanged is used you still have to fire the PropertyChanged event from the correct thread.
Dependecy properties have built in notification support and does not require wpf to do any reflection, so they are faster for data binding (but slower to set/get albeit on a small time scale)
your scenario might be diffrent than mine, but its someting to look at i think :)
您可以使用 .NET 4.5 名为“CallerMemberName”的新功能来避免对属性名称进行硬编码。
You can use the new feature of .NET 4.5 named "CallerMemberName" to avoid hard-coding the property name.