MVVM 消息传递或事件或其他什么选项?

发布于 2024-10-08 08:46:05 字数 272 浏览 0 评论 0原文

我在 MainViewModel 中有一个菜单,现在在选择特定的菜单项时我想更新已加载的视图的数据。

即,虽然 MainViewModel 中有该 viewModel 的实例,但当我尝试通过该实例调用方法并更改数据属性时,它不会显示视图中的更改。而当我使用该 viewModel 视图上的按钮通过中继命令调用该方法时,会发生相同的更改。

现在我需要从 MainViewModel 调用该 viewModel 的中继命令,我想这会解决问题,但如何做到这一点?什么是最简单的方法。我需要使用消息传递吗?

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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单 2024-10-15 08:46:05

我尝试了 MVVM Light Messenger 类,它非常简单和优雅(保持 ViewModel 松散耦合)!最重要的是它有效

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!
}

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!
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文