简体   繁体   English

Silverlight-视图模型之间的通信

[英]Silverlight - communicate between viewmodels

This UI consists of a Main.xaml which contains a navigation frame which holds (Person.xaml), so there's two different viewmodels involved. 该UI由Main.xaml组成,该Main.xaml包含一个包含(Person.xaml)的导航框架,因此涉及两个不同的视图模型。

在此处输入图片说明

I would like to filter the content of the datagrid when the menu in Main.xaml is clicked. 单击Main.xaml中的菜单时,我想过滤datagrid的内容。

  • People (shows all) 人(显示全部)
  • Score above 50 得分高于50
  • Score below 50 得分低于50

My approach is to use the MVVM light Messaging by having PersonViewModel subscribe on a certain message which is sent from MainViewModel. 我的方法是通过让PersonViewModel订阅从MainViewModel发送的特定消息来使用MVVM轻信息。 This would work, but are there any other ways of doing this (best practice)? 这会起作用,但是还有其他方法可以做到这一点(最佳实践)吗?

I'd hate to implement the MvvmLight Messaging for all my scenarios where UI elements from different viewmodels have a need to communicate, if there is a better way of solving this. 如果不存在解决此问题的更好方法,我将不愿为来自不同视图模型的UI元素需要交流的所有场景实施MvvmLight Messaging。

If you are using MVVM light, I guess you have created these viewmodels in the ViewModelLocator. 如果您使用的是MVVM light,我想您已经在ViewModelLocator中创建了这些viewmodel。 So just use it to get access among viewmodels in your application. 因此,只需使用它即可在应用程序的视图模型之间进行访问。

Another approach is to use some type of IoC container like Unity or SimpleIoC. 另一种方法是使用某些类型的IoC容器,例如Unity或SimpleIoC。

I hope this helps you. 我希望这可以帮助你。

We use MVVM light also. 我们也使用MVVM灯。 From my understanding of your requirements. 根据我对您要求的理解。 you can probably just use a single DomainContext and then loading all the entities to it and then querying the context for each click item. 您可以只使用一个DomainContext,然后将所有实体加载到其中,然后为每个单击项查询上下文。

once you have a Context, you can basically query it dynamically. 一旦有了上下文,基本上就可以动态查询它。 Here's something from one of my code. 这是我的代码之一。

 GetUserBUGroups = SecurityDomainContext.Current.UserBUGroups.Where(ub => ub.UserID == GetUsers.UserID).OrderBy(o => o.BUGroup.BUGroupDesc).ToList();

For simple scenarios such as this, it does not seem appropriate to use several ViewModels. 对于诸如此类的简单方案,似乎不适合使用多个ViewModel。 I believe you should insert the filter options directly in the PersonViewModel 我相信您应该直接在PersonViewModel中插入过滤器选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM