Sam*_*Sam 7 c# wpf mvvm viewmodel mvvm-light
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 没有实例化,以便在加载视图之前能够接收消息(此时消息已经被广播)!
那么,解决这个问题的最佳方法是什么?到目前为止,我已经考虑过CustomerViewModel在实例化后向ViewAllCustomersViewModel发送请求(基本上说"我准备接收消息"),然后ViewAllCustomersViewModel将ID发送回CustomerViewModel ......但这是一个解决这个问题的必要方法?这对我来说似乎有点难看!
否则,我在想是否有另一种沟通方式可以解释我遇到的问题?但是,这不是消息系统的全部意义......能够在视图模型之间进行通信吗?或者我可以强制在启动时实例化视图模型吗?如果是这样,那将如何影响ViewModelLocator?
我希望我已经清楚地概述了这个问题,为了解释的目的,我使用了虚构的视图模型名称...请随时编辑或建议您希望我添加的任何其他信息!
您是否尝试通过您的模型进行交流?直到最后我才能阅读您的主题,但这是我在ViewModels之间进行通信的方式.两个View Models都具有会话实例.
public ViewModel1(ISession session)
{
_session = session;
}
public ViewModel2(ISession session)
{
_session = session;
}
Run Code Online (Sandbox Code Playgroud)
这样,当您在BDD(行为驱动开发)中测试应用程序时,您可以在没有视图的情况下测试应用程序.胶水是模型.
正如您在此图片中看到的那样,您应该能够在没有视图的情况下测试您的应用程序.

| 归档时间: |
|
| 查看次数: |
6718 次 |
| 最近记录: |