Prism 框架中使用IEventAggregator事件聚合器简单实现ViewModel之间的通讯
2017-07-10 12:37
513 查看
1.在项目中添加EventAggregator类库,添加EventAggregatorRepository类和GetInputMessages类。
其中EventAggregatorRepository提供事件聚合实例,GetInputMessages为定义的事件类型。
2.订阅事件
添加对EventAggregator的引用,在XXViewModel初始化时订阅
3.发布事件
在其他ViewModel中添加对EventAggregato的引用,发布事件
以上就能实现不同ViewModel间传递参数
using Microsoft.Practices.Prism.PubSubEvents; using Microsoft.Practices.Prism.Regions; using System; namespace EventAggregatorPratice { public class EventAggregatorRepository { public EventAggregatorRepository() { eventAggregator = new EventAggregator(); } public IEventAggregator eventAggregator; public static EventAggregatorRepository eventRepository = null; //单例,保持内存唯一实例 public static EventAggregatorRepository GetInstance() { if (eventRepository == null) { eventRepository = new EventAggregatorRepository(); } return eventRepository; } } }
using Microsoft.Practices.Prism.PubSubEvents; namespace EventAggregatorPratice { public class GetInputMessages : PubSubEvent<string> { } }
其中EventAggregatorRepository提供事件聚合实例,GetInputMessages为定义的事件类型。
2.订阅事件
添加对EventAggregator的引用,在XXViewModel初始化时订阅
public void SetSubscribe() { EventAggregatorRepository .GetInstance() .eventAggregator .GetEvent<GetInputMessages>() .Subscribe(ReceiveMessage, ThreadOption.UIThread, true); } public void ReceiveMessage(string messageData) { //dosomething } public XXViewModel() { InitializeComponent(); SetSubscribe(); }
3.发布事件
在其他ViewModel中添加对EventAggregato的引用,发布事件
public static void Print(string Meesage) { EventAggregatorRepository .GetInstance() .eventAggregator .GetEvent<GetInputMessages>() .Publish(Meesage); }
以上就能实现不同ViewModel间传递参数
相关文章推荐
- 利用Prism的IEventAggregator事件聚合器来实现ViewModel之间的通讯
- 使用Net类库,实现的基于TCP/IP简单的通讯框架框架(一)
- ios开源框架——WebViewJavascriptBridge的简单使用 实现JS与iOS Native Code互调
- SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论 SignalR 简单示例 通过三个DEMO学会SignalR的三种实现方式 SignalR推送框架两个项目永久连接通讯使用 SignalR 集线器简单实例2 用SignalR创建实时永久长连接异步网络应用程序
- Netty框架的简单使用,实现socket通讯
- Prism的IEventAggregator事件聚合器, 事件订阅发布, ViewModel之间的通讯
- 『原创』PPC和PC使用TCP通讯——简单实现
- VB中使用DDE实现两个应用程序之间的通讯
- Android中实现滑动翻页—使用ViewFlipper(dp和px之间进行转换)
- MVVMLight Toolkit在Windows Phone中的使用扩展之一:在ViewModel中实现导航,并传递参数
- 使用UIScrollView和UIPageControl实现一个简单的图片浏览功能
- [Silverlight入门系列]使用MVVM模式(7):ViewModel的INotifyPropertyChanged接口实现
- 在模型和控制器之间使用ViewModel
- 在MVVMLight框架的ViewModel中实现NavigationService
- 使用UIScrollView和UIPageControl实现一个简单的图片浏览功能
- 扩展ASP.NET MVC三层框架并使用StructureMap实现依赖注入1-Model层的实现
- 在Qt的Model/View框架中实现Drag-Drop操作
- Qt GraphicsView框架中实现多个item之间的层次调整功能
- android游戏开发框架libgdx的使用(十八)—简单的AVG游戏效果实现
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信