MVVM消息传递或事件或其他什么选择? [英] MVVM messaging or events or what other option out therer?
问题描述
我在MainViewModel中有一个菜单,现在选择了一个特定的menuItem,我想更新已经加载的视图的数据.
I have a menu in MainViewModel, now on selection of a particular menuItem I wanted to update data of a view which is already loaded.
即尽管MainViewModel中有一个viewModel实例,但是当我尝试通过该实例调用方法并更改data属性时,它不会在视图中显示更改.但是,当我使用viewModel的视图上的按钮通过Relay命令调用该方法时,会发生相同的变化.
i.e. although there's an instance of that viewModel in MainViewModel, when I try to invoke the method thru that instance and change the data property, it doesnt show the changes in the view. Whereas same changes occur when I invoke that method through relay command using a button on that viewModel's view.
现在就像需要从MainViewModel调用该viewModel的中继命令一样,我想这将解决问题,但是该怎么做呢?最简单的方法.我需要使用消息传递吗?
Now its like I need to invoke relay command of that viewModel from MainViewModel, I guess that will fix the problem, but how to do that? what's easiest way. Will i need to use messaging?
推荐答案
我尝试了MVVM Light Messenger类,它非常简单明了(保持ViewModels松散耦合)!最重要的是它可以工作
I tried MVVM Light messenger class, its quite straightforward and elegant (keeps ViewModels loosely coupled)!! and most importantly it works
code:
Send:
Messenger.Default.Send(stringParameter, "key_anything");
Register:
Messenger.Default.Register<string>(this, "key_anything", invokeFunction);
private void invokeFunction(string stringParamter)
{
//code goes here!
}
这篇关于MVVM消息传递或事件或其他什么选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!