MVVM light-如何在其他视图模型中访问属性 [英] MVVM light - how to access property in other view model

查看:90
本文介绍了MVVM light-如何在其他视图模型中访问属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mvvm light来构建Silverlight应用程序. 是否有一个代码片段显示了如何从另一个视图模型或后面的用户控件的代码中访问视图模型的属性或命令?

I'm using mvvm light to build a Silverlight application. Is there a code snippet that shows how to access a view model's property or command from within another view model or user control's code behind?

我想这很简单,但是我以某种方式错过了一些东西.

I guess it's simple, but I somehow missed something.

Ueli

推荐答案

您可以使用Messenger来做到这一点: 在UserViewModel中发送用户:

You could use the Messenger to do this: Send the user in the UserViewModel:

Messenger.Send<User>(userInstance);

只会将用户发送给任何有兴趣的人.

would just send the user to anyone interested.

并在CardViewModel中注册收件人:

And register a recipient in your CardViewModel:

Messenger.Register<User>(this, delegate(User curUser){_curUser = curUser;});

或者您也可以从CardViewModel发送一个请求来喊叫用户:

or you can also send a request from your CardViewModel for shouting the user:

Messenger.Send<String, UserViewModel>("Gimme user");

并在UserViewModel中对此做出反应:

And react on that in the UserViewModel:

Messenger.Register<String>(this, delegate(String msg)
{
if(msg == "Gimme user")
Messenger.Send<User>(userInstance);
});

(在实际情况下,最好使用枚举而不是字符串:))

(You better use an enum and not a string in a real scenario :) )

也许您甚至可以直接回复,但我目前无法检查.

Perhabs you can even response directly but I can't check it at the moment.

只需检查一下: Mvvm轻信使

这篇关于MVVM light-如何在其他视图模型中访问属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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