Prism 订阅事件 IEventAggregator 说明
本节学习了Event Aggregation事件聚合,这个在Prism中很重要,特别是对于Module间的通信。除了前面介绍的Command可以用于模块间的通信,还有我们这一节介绍的Event Aggregation(事件聚合).
(一)为什么不用.NET FrameWork中的事件呢?
使用.NET Framework事件是罪简单和直观的方式用于非松散耦合需求的组件,属于对象引用依赖的发布-订阅模型
(二) EventAggregator事件聚合器
提供了多点传送发布/订阅功能。这意味着可能有可以触发同一事件多个发布者和可以监听同一事件的订阅者。
(三)模块间通信过程简介
CompositePresentationEvent<TPayload>类实例实现了事件的订阅和取消,而IEventAggregator实例用来获取接收CompositePresentationEvent<TPayload>类实例.IEventAggregator实例在每个模块中含有,这样模块间就可以通信了。
(四)下面贴出Prism中Event Aggregation QuickStart的部分代码:
(1)创建了 CompositePresentationEvent<TPayload>类
在项目中EventAggregation.Infrastructure.Silverlight的FundAddedEvent.cs代码中,
//定义CompositePresentationEvent<TPayload>类
//(1)该类是泛型类:强制发布者和订阅者要一种正确的类型实现 发布-订阅连接
//(2)是唯一继承自EventBase的类
//(3)因为CompositePresentationEvent<TPayload>往往被多个模块公用,所以要单独于
// 其他的模块新建类库项目
//FundOrder是这里的TPayLoad类型
public class FundAddedEvent : CompositePresentationEvent<FundOrder>
{
}
(2)事件的发布
在ModuleA.Silverlight项目中的
public class AddFundPresenter
{
private IAddFundView _view;
//IEventAggregator 事件聚合器接口
private IEventAggregator eventAggregator; public AddFundPresenter(IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
} //事件发布:当用户添加一个fund基金,事件就被发布
//发布者发布一个事件
//(1)通过IEventAggregator的实例eventAggregator的Publish方法
//(2)指定GetEvent<TEventType>中TEventType为FunAddedEvent
void AddFund(object sender, EventArgs e)
{
FundOrder fundOrder = new FundOrder();
fundOrder.CustomerId = View.Customer;
fundOrder.TickerSymbol = View.Fund; if (!string.IsNullOrEmpty(fundOrder.CustomerId) && !string.IsNullOrEmpty(fundOrder.TickerSymbol))
eventAggregator.GetEvent<FundAddedEvent>().Publish(fundOrder);
} public IAddFundView View
{
get { return _view; }
set
{
_view = value;
_view.AddFund += AddFund;
}
} }
(3)事件的订阅
在ModuleB.Silverlight项目中的ActivityPresenter.cs中
public class ActivityPresenter
{
private string _customerId;
private IEventAggregator eventAggregator;
private SubscriptionToken subscriptionToken; public ActivityPresenter(IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
} public void FundAddedEventHandler(FundOrder fundOrder)
{
Debug.Assert(View != null);
View.AddContent(fundOrder.TickerSymbol);
} public bool FundOrderFilter(FundOrder fundOrder)
{
return fundOrder.CustomerId == _customerId;
} public IActivityView View { get; set; } public string CustomerId
{
get
{
return _customerId;
} set
{
_customerId = value; FundAddedEvent fundAddedEvent = eventAggregator.GetEvent<FundAddedEvent>(); if (subscriptionToken != null)
{
fundAddedEvent.Unsubscribe(subscriptionToken);
}
//订阅事件
//(1)获取事件聚合器实例
//(2)调用Subscribe方法
// Subscribe方法重载,有不同的作用
// Action<T>: 泛型委托
// ThreadOption:当为PublisherThread时(默认值)能获取发布者线程
// BackgroundThread时 从.NET Framework线程池上异步获取事件
// UIThread时 获取事件从UI线程上。
// keepSubscriberReferenceAlive: 当为true 事件实例是强引用订阅者,因此不能垃圾回收
// 当为false(默认值)弱引用订阅者,因此当没有其他引用时允许垃圾回收释放订阅者实例,当订阅者实例被回收,事件自动取消订阅
subscriptionToken = fundAddedEvent.Subscribe(FundAddedEventHandler, ThreadOption.UIThread, false, FundOrderFilter);
//lamda表达式写法
//subscriptionToken = fundAddedEvent.Subscribe(FundAddedEventHandler, ThreadOption.UIThread, false, FunOrder => FunOrder.CustomerId == this._customerId);
View.SetTitle(string.Format(CultureInfo.CurrentCulture, Resources.ActivityTitle, CustomerId));
}
}
}
(1)获取事件聚合器实例
(2)调用Subscribe方法
Subscribe方法重载,有不同的作用
Action<T>: 泛型委托
ThreadOption:
当为PublisherThread时(默认值)能获取发布者线程
BackgroundThread时 从.NET Framework线程池上异步获取事件
UIThread时 获取事件从UI线程上。
keepSubscriberReferenceAlive: 当为true 事件实例是强引用订阅者,因此不能垃圾回收
当为false(默认值)弱引用订阅者,因此当没有其他引用时允许垃圾回收释放订阅者实例,当订阅者实例被回收,事件自动取消订阅
Prism 订阅事件 IEventAggregator 说明的更多相关文章
- 中控考勤仪IFace302多线程操作时无法订阅事件
场景: 在各办事点安装中控考勤仪Iface302,各办事点的工作人员上下班报到时使用指纹或面纹进行自动登记,验证成功后将与服务吕进行通讯记录相关的考勤信息. 条件限制: 由于Iface302设备不支持 ...
- 微信公众平台开发(三) 订阅事件(subscribe)处理
一.简介 新用户关注微信公众平台,将产生一个订阅事件,即subscribe事件,默认代码中没有对这一事件进行相应回复处理. 在新用户关注公众平台后,可能想知道该平台提供了哪些功能,以及怎样使用该平台, ...
- 微信公众平台开发3:订阅事件subscribe处理
新用户关注微信公众平台,将产生一个订阅事件,即subscribe事件,默认代码中没有对这一事件进行相应处理. 在新用户关注公众平台后,可能想知道该平台提供了哪些功能,以及怎样使用该平台,通俗一点讲就是 ...
- C#winform使用+=和-=订阅事件和移除事件订阅
1.C#winform中使用+=和-=订阅事件和移除事件订阅 2.可以使用+=给一个控件订阅多个事件,触发事件时按顺序执行,直到使用-=移除事件订阅为止.
- [No0000130]WPF 4.5使用标记扩展订阅事件
自从我上次写到关于标记扩展的时候已经有一段时间了...... Visual Studio 11 Developer Preview的发布给WPF带来了一些新功能,让我有理由再次使用它们.我要在这里讨论 ...
- Android 使用RxJava实现一个发布/订阅事件总线
1.简单介绍 1.1.发布/订阅事件主要用于网络请求的回调. 事件总线可以使Android各组件之间的通信变得简单,而且可以解耦. 其实RxJava实现事件总线和EventBus比较类似,他们都依据与 ...
- 如何监视 WPF 中的所有窗口,在所有窗口中订阅事件或者附加 UI
原文:如何监视 WPF 中的所有窗口,在所有窗口中订阅事件或者附加 UI 由于 WPF 路由事件(主要是隧道和冒泡)的存在,我们很容易能够通过只监听窗口中的某些事件使得整个窗口中所有控件发生的事件都被 ...
- Prism.WPF -- Prism框架使用(下)
本文参考Prism官方示例 命令使用 Prism提供了两种命令:DelegateCommand和CompositeCommand. DelegateCommand DelegateCommand封装了 ...
- Prism的IEventAggregator事件聚合器, 事件订阅发布, ViewModel之间的通讯
WPF中时常会遇到ViewModel之间的通讯,ViewModel并不知道自己的View,但是一个View发生的更改需要通知另外一个View. 举一个例子,软件界面上有个人信息,打开一个界面更改用户的 ...
随机推荐
- oracle 之创建视图异常
最近在整理的oracle 的时候发现.创建视图 例如: CREATE OR REPLACE VIEW dept_sum_vw(name,minsal,maxsal,avgsal) AS SELECT ...
- Codeforces 837D - Round Subset DP
先算出每个数的pop1(twonum),pop(fivenum)然后DP ans[i][j]表示选i个数有j个2时最多有多少个5 转移方程是 ;j--) { ;w++) { ans[j][w]=max ...
- cypress
https://community.cypress.com/docs/DOC-14090 usb3.0 only支持
- JavaScript入门学习之一——初级语法
JavaScript是前端编辑的一种编程语言(不同于html,html是一种标记语言),所以和其他的编程语言一样,我们将会从下面几点学习 基础语法 数据类型 函数 面向对象 JavaScript的组成 ...
- 获取页面url信息
方法: window.location.href = prefixURL+'webstatic/messageAnalysis/datadetail.html?id=' + num + "& ...
- dlsym用法
1. 包含头文件 #include<dlfcn.h> 2. 函数定义 void *dlsym(void *handle, const char* symbol); handle是使用dlo ...
- 一个tornado框架下的文件上传案例
html部分----使用了form表单,注意三要素 method="post" action="/loaddata" enctype="multip ...
- Acwing-279-自然数拆分(背包)
链接: https://www.acwing.com/problem/content/281/ 题意: 给定一个自然数N,要求把N拆分成若干个正整数相加的形式,参与加法运算的数可以重复. 求拆分的方案 ...
- 手动编译源码安装包报错 fatal error:cruses.h: no such file or direcrory
很明显是缺少cruses.h这个文件,但是用yum搜索又搜不到,可能是我的yum源的库包太少的原因吧. 后来多方查找,发现cruses.h这个头文件属于ncurses模块,需要安装ncurses-de ...
- [洛谷P1607] 庙会班车
题目描述 Although Farmer John has no problems walking around the fair to collect prizes or see the shows ...