我应该如何在ViewModels之间进行交流? [英] How should I communicate between ViewModels?

查看:80
本文介绍了我应该如何在ViewModels之间进行交流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVVM Light,并使用打包的Messenger系统在视图模型之间进行通信,但是我遇到了一些难题!基本上,当用户单击客户记录时,将打开相应的视图,并实例化 CustomerViewModel .此时, CustomerViewModel 需要从先前的视图模型( ViewAllCustomersViewModel )中选择的客户ID,以便它可以获取该视图绑定到的所选客户信息(仍在吗?) .因此,起初我的想法还是将该ID从 ViewAllCustomersViewModel (选择要查看客户的位置)的消息中发送给 CustomerViewModel ...但是,不会实例化为能够接收消息!

I am using MVVM Light and have used the packaged messenger system to communicate between view models, however I have hit a bit of a dilemma! Basically when a user clicks on a customer record the corresponding view is opened and with it the CustomerViewModel is instantiated. At this point the CustomerViewModel requires the selected customers ID from the previous view model (ViewAllCustomersViewModel) so that it can get selected customers info which the view binds to (still following?). So initially my thought was too send that ID in a message from the ViewAllCustomersViewModel (where the customer to be viewed is selected) to the CustomerViewModel... HOWEVER, the CustomerViewModel is not instantiated to be able to receive the message until the view is loaded (at which point the message has already been broadcast)!

那么,解决此问题的最佳方法是什么?到目前为止,我已经考虑过 CustomerViewModel 在实例化后将请求发送到 ViewAllCustomersViewModel (基本上是说我准备好接收消息"),然后 ViewAllCustomersViewModel 将ID发送回 CustomerViewModel ...,但这是解决此问题的必要方法吗?对我来说似乎有点难看!

So, what would be the best way to solve this issue? So far I have considered the CustomerViewModel sending a request to the ViewAllCustomersViewModel once it has been instantiated (basically saying "I am ready to receive the message"), and then the ViewAllCustomersViewModel sending the ID back to the CustomerViewModel... but is this a necessary approach to solve this? It seems a bit ugly to me!

否则,我在想是否还有另一种沟通方式可以解决我遇到的问题?但是,这是否不是消息传递系统的全部要点...能够在视图模型之间进行通信?还是可以强制在启动时实例化视图模型?如果是这样,这会对 ViewModelLocator 产生什么影响?

Otherwise, I was thinking is there another way to communicate which can account for the issue I am having? But then isn't this the whole point of the messaging system... to be able to communicate between view models? OR can I force the view model to be instantiated on start up? If so, how would that affect the ViewModelLocator?

我希望我已经清楚地概述了这个问题,出于解释的目的,我使用了虚构的视图模型名称...,请随时编辑或建议您希望我添加的任何其他信息!

I hope I have outlined the issue clearly, I have used fictional view model names for the purpose of the explanation... and please feel free to edit or suggest any additional information that you would like me to add!

推荐答案

您是否尝试通过模型进行通信?直到最后我都无法阅读您的主题,但这是我在ViewModel之间进行交流的方式. 两种视图模型都有会话实例.

Did you try to communicate via your model? I was not able to read your topic until the end but this is how I communicate between ViewModels. Both View Models have the instance of session.

public ViewModel1(ISession session)
        {
            _session = session;           
        }

public ViewModel2(ISession session)
        {
            _session = session;           
        }

这样,当您在BDD(行为驱动的开发)中测试应用程序时,可以在没有视图的情况下测试应用程序.胶水就是模型.

This way, when you test your application in BDD (behavior driven development), you can test your application without the view. The glue is the model.

正如在此图片上看到的,您应该能够在没有视图的情况下测试您的应用程序.

As you can see on this picture, you should be able to test your application without the view.

这篇关于我应该如何在ViewModels之间进行交流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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