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

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

问题描述

我正在使用 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.

尤利

推荐答案

您可以使用 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 灯 - 如何访问其他视图模型中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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