MVVM ViewModel到模型的通信 [英] MVVM ViewModel to Model comminication

查看:91
本文介绍了MVVM ViewModel到模型的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVVM中,我可以使用  INotifyPropertyChanged 命令清楚地理解查看
ViewModel 之间的通信

In MVVM I can clearly understand communication between View and ViewModel using INotifyPropertyChanged and Commands.

ViewModel 模型的沟通情况如何,反之亦然。对我来说,目前还不清楚哪种方法最适合MVVM架构。 

But what about ViewModel to Model communication and vice versa. For me it is unclear what approach is best for MVVM architecture. 

我应该从 ViewModel $ b调用模型方法$ b直接?我如何以正确的方式跟踪模型 - 的变化?

Should I just call Model methods from ViewModel directly? And how I shoud track changes in Model - the right way?

任何具体的例子都将受到赞赏

Any concrete examples would be appreciated

推荐答案

>>我应该只是打电话给模特 来自ViewModel的方法直接?

>>Should I just call Model methods from ViewModel directly?

是的,直接访问。视图模型充当视图和模型之间的中介, 视图模型从模型中检索数据,然后使数据可用于视图。

Yes, directly access. The view model acts as an intermediary between the view and the model,  the view model retrieves data from the model and then makes the data available to the view.

模型在MVVM模式封装了业务逻辑,验证逻辑和数据。  模型对象的示例包括存储库,业务对象,数据传输对象(DTO),普通旧CLR对象(PO​​CO)以及生成的实体和代理
对象。

The model in the MVVM pattern encapsulates business logic, validation logic and data.  Examples of model objects include repositories, business objects, data transfer objects (DTOs), Plain Old CLR Objects (POCOs), and generated entity and proxy objects.

https://msdn.microsoft.com/en-us/library/gg405484%28PandP.40%29.aspx?f=255&MSPPError=-2147217396

<强> >>我应该如何追踪模型中的变化 - 正确的方式?

>> And how I should track changes in Model - the right way?

通常,并非所有模型都需要通知PropertyChanged。

Generally, not all the model need notify PropertyChanged.

如果您的模型在更改时需要更新UI,那么您的模型需要实现InotifyPropertyChanged。

If your model need update UI when changed, then your model need implement InotifyPropertyChanged.

当您使用POCO课程时;它不提供任何PropertyChanged事件。 
您可以在视图模型中重新打包视图所需的模型。

When you use a POCO class which does not provide any PropertyChanged events.  You can repackage the model that a view needs in the view model.

也许以下示例可以帮助您。

Maybe following samples can help you.

https://gallery.technet.microsoft.com/Easy-MVVM-Examples-48c94de3

https://www.codeproject.com/Articles / 819294 / WPF-MVVM-step-by-step-Basics-to-Advance-Level

https://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial

最诚挚的问候,

Bob


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

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