选择绑定的ViewModel属性或使用MVVM Light Toolkit在ViewModel和View之间进行通信的消息传递之间进行选择 [英] Choosing between bound ViewModel properties or messaging to communicate between ViewModel and View using the MVVM Light Toolkit

查看:109
本文介绍了选择绑定的ViewModel属性或使用MVVM Light Toolkit在ViewModel和View之间进行通信的消息传递之间进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVVM Light工具箱(我很喜欢).目前,我已经有消息传递了一些来自ViewModel的交互,供View使用.通常,这些类型的消息表明View应该执行诸如隐藏自身,显示已保存数据的确认消息之类的操作.

I'm using MVVM Light toolkit (which I love). I currently have messaging in place for some interaction originating from the ViewModel and intended for consumption by the View. Typically these types of messages indicate the View should do something like hide itself, show a confirmation message that data was saved, etc.

这有效.在View的构造函数中,我向Messenger注册:

This works. In the constructor for the View, I register with the Messenger:

Messenger.Default.Register<NotificationMessage<PaperNotification>>(this, n => HandlePaperNotification(n));

当我使用Messenger交流ViewModel之间的跨领域问题(例如身份)时,我可以看到,当在ViewModelLocator中清理ViewModel时,ViewModels的基类(ViewModelBase)取消注册任何订阅的消息.我不需要做任何事情,因为MVVM Light Toolkit可以为我处理这些事情.但是,当我在视图中使用它们时,我必须在关闭时明确注销它们,就像这样:

When I'm using the Messenger to communicate cross-cutting concerns between ViewModels (like identity), I can see that when the ViewModel is cleaned up in the ViewModelLocator, the base class for ViewModels (ViewModelBase) unregisters any subscribed messages. I don't have to do anything, as MVVM Light Toolkit handles that for me. However, when I use them in the Views, I have to expressly unregister them at Closing time, like so:

Messenger.Default.Unregister(this);

我想我可以为Views继承一个基类.

I suppose I could implement a base class for Views to inherit from.

但是,让我吃惊的是,在View中使用Messenger可能是一种代码味道...它可以工作,但可能不是最佳方法.我想知道是否应该在ViewModel上创建一个属性并将View元素的任何部分绑定到该属性.在隐藏表单的示例中,属性可以是一个名为"Show"的布尔值.考虑到这一点,我可以看到,在许多情况下,这将导致必须编写ValueConverter.一种方法似乎不可测.另一种方法似乎需要更多的代码,也许还需要引入多余的ValueConverters,这本身可能会变成代码的味道.

However, it strikes me that perhaps this is a code smell to be using the Messenger in the View... it works, but it might not be the best way. I'm wondering if I should instead create a property on the ViewModel and bind whatever part of the View's elements to it. In the example of hiding a form, a property could be a boolean called "Show". As I think about it, I can see that in many cases this will result in having to write a ValueConverter. One way seems less testable. The other way seems to require much more code and perhaps the introduction of excess ValueConverters, which could become a code smell in themselves.

所以(经过所有步骤之后),我的问题是:

So (after all that build up) my question is this:

在View中使用消息是更可取的还是添加属性(以及可能的ValueConverters)以使ViewModel以更可绑定的方式驱动它更好?

Is it preferable to use messages within the View or is it better to add properties (and potentially ValueConverters) to allow the ViewModel to drive it in a more bindable fashion?

推荐答案

在MVVM中. ViewModel通过DataBinding和Command与View通信.如果您需要其他功能,则需要使用此方法来实现. 消息传递应该仅用于ViewModels.视图应该是ViewModel中数据的愚蠢"可视化工具.

In MVVM. ViewModel comunicates with View through DataBinding and Commands. If you need some other functionality, you need to implement it using this means. Messaging is supposed to be only for ViewModels. Views are supposed to be "stupid" visualisers of your data in ViewModel.

这篇关于选择绑定的ViewModel属性或使用MVVM Light Toolkit在ViewModel和View之间进行通信的消息传递之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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