.NET Core 3 WPF MVVM框架 Prism系列之事件聚合器
本文将介绍如何在.net core3环境下使用mvvm框架prism的使用事件聚合器实现模块间的通信
一.事件聚合器
在上一篇 .net core 3 wpf mvvm框架 prism系列之模块化 我们留下了一些问题,就是如何处理同模块不同窗体之间的通信和不同模块之间不同窗体的通信,prism提供了一种事件机制,可以在应用程序中低耦合的模块之间进行通信,该机制基于事件聚合器服务,允许发布者和订阅者之间通过事件进行通讯,且彼此之间没有之间引用,这就实现了模块之间低耦合的通信方式,下面引用官方的一个事件聚合器模型图:
二.创建和发布事件
1.创建事件
首先我们来处理同模块不同窗体之间的通讯,我们在prismmetrosample.infrastructure新建一个文件夹events,然后新建一个类patientsentevent,代码如下:
patientsentevent.cs:
public class patientsentevent: pubsubevent<patient> { }
2.订阅事件
然后我们在病人详细窗体的patientdetailviewmodel类订阅该事件,代码如下:
patientdetailviewmodel.cs:
public class patientdetailviewmodel : bindablebase { ieventaggregator _ea; imedicineserivce _medicineserivce; private patient _currentpatient; //当前病人 public patient currentpatient { get { return _currentpatient; } set { setproperty(ref _currentpatient, value); } } private observablecollection<medicine> _lstmedicines; //当前病人的药物列表 public observablecollection<medicine> lstmedicines { get { return _lstmedicines; } set { setproperty(ref _lstmedicines, value); } } //构造函数 public patientdetailviewmodel(ieventaggregator ea, imedicineserivce medicineserivce) { _medicineserivce = medicineserivce; _ea = ea; _ea.getevent<patientsentevent>().subscribe(patientmessagereceived);//订阅事件 } //处理接受消息函数 private void patientmessagereceived(patient patient) { this.currentpatient = patient; this.lstmedicines = new observablecollection<medicine>(_medicineserivce.getrecipesbypatientid(this.currentpatient.id).firstordefault().lstmedicines); } }
3.发布消息
然后我们在病人列表窗体的patientlistviewmodel中发布消息,代码如下:
patientlistviewmodel.cs:
public class patientlistviewmodel : bindablebase { private iapplicationcommands _applicationcommands; public iapplicationcommands applicationcommands { get { return _applicationcommands; } set { setproperty(ref _applicationcommands, value); } } private list<patient> _allpatients; public list<patient> allpatients { get { return _allpatients; } set { setproperty(ref _allpatients, value); } } private delegatecommand<patient> _mousedoubleclickcommand; public delegatecommand<patient> mousedoubleclickcommand => _mousedoubleclickcommand ?? (_mousedoubleclickcommand = new delegatecommand<patient>(executemousedoubleclickcommand)); ieventaggregator _ea; ipatientservice _patientservice; /// <summary> /// 构造函数 /// </summary> public patientlistviewmodel(ipatientservice patientservice, ieventaggregator ea, iapplicationcommands applicationcommands) { _ea = ea; this.applicationcommands = applicationcommands; _patientservice = patientservice; this.allpatients = _patientservice.getallpatients(); } /// <summary> /// datagrid 双击按钮命令方法 /// </summary> void executemousedoubleclickcommand(patient patient) { //打开窗体 this.applicationcommands.showcommand.execute(flyoutnames.patientdetailflyout); //发布消息 _ea.getevent<patientsentevent>().publish(patient); } }
效果如下:
4.实现多订阅多发布
同理,我们实现搜索后的medicine添加到当前病人列表中也是跟上面步骤一样,在events文件夹创建事件类medicinesentevent:
medicinesentevent.cs:
public class medicinesentevent: pubsubevent<medicine> { }
在病人详细窗体的patientdetailviewmodel类订阅该事件:
patientdetailviewmodel.cs:
public patientdetailviewmodel(ieventaggregator ea, imedicineserivce medicineserivce) { _medicineserivce = medicineserivce; _ea = ea; _ea.getevent<patientsentevent>().subscribe(patientmessagereceived); _ea.getevent<medicinesentevent>().subscribe(medicinemessagereceived); } /// <summary> // 接受事件消息函数 /// </summary> private void medicinemessagereceived(medicine medicine) { this.lstmedicines?.add(medicine); }
在药物列表窗体的medicinemaincontentviewmodel也订阅该事件:
medicinemaincontentviewmodel.cs:
public class medicinemaincontentviewmodel : bindablebase { imedicineserivce _medicineserivce; ieventaggregator _ea; private observablecollection<medicine> _allmedicines; public observablecollection<medicine> allmedicines { get { return _allmedicines; } set { setproperty(ref _allmedicines, value); } } public medicinemaincontentviewmodel(imedicineserivce medicineserivce,ieventaggregator ea) { _medicineserivce = medicineserivce; _ea = ea; this.allmedicines = new observablecollection<medicine>(_medicineserivce.getallmedicines()); _ea.getevent<medicinesentevent>().subscribe(medicinemessagereceived);//订阅事件 } /// <summary> /// 事件消息接受函数 /// </summary> private void medicinemessagereceived(medicine medicine) { this.allmedicines?.add(medicine); } }
在搜索medicine窗体的searchmedicineviewmodel类发布事件消息:
searchmedicineviewmodel.cs:
ieventaggregator _ea; private delegatecommand<medicine> _addmedicinecommand; public delegatecommand<medicine> addmedicinecommand => _addmedicinecommand ?? (_addmedicinecommand = new delegatecommand<medicine>(executeaddmedicinecommand)); public searchmedicineviewmodel(imedicineserivce medicineserivce, ieventaggregator ea) { _ea = ea; _medicineserivce = medicineserivce; this.currentmedicines = this.allmedicines = _medicineserivce.getallmedicines(); } void executeaddmedicinecommand(medicine currentmedicine) { _ea.getevent<medicinesentevent>().publish(currentmedicine);//发布消息 }
效果如下:
然后我们看看现在demo项目的事件模型和程序集引用情况,如下图:
我们发现patientmodule和medicinemodule两个模块之间做到了通讯,但却不相互引用,依靠引用prismmetrosample.infrastructure程序集来实现间接依赖关系,实现了不同模块之间通讯且低耦合的情况
三.取消订阅事件
prism还提供了取消订阅的功能,我们在病人详细窗体提供该功能,patientdetailviewmodel加上这几句:
patientdetailviewmodel.cs:
private delegatecommand _canclesubscribecommand; public delegatecommand canclesubscribecommand => _canclesubscribecommand ?? (_canclesubscribecommand = new delegatecommand(executecanclesubscribecommand)); void executecanclesubscribecommand() { _ea.getevent<medicinesentevent>().unsubscribe(medicinemessagereceived); }
效果如下:
四.几种订阅方式设置
我们在demo已经通过消息聚合器的事件机制,实现订阅者和发布者之间的通讯,我们再来看看,prim都有哪些订阅方式,我们可以通过pubsubevent类上面的subscribe函数的其中最多参数的重载方法来说明:
subscribe.cs:
public virtual subscriptiontoken subscribe(action<tpayload> action, threadoption threadoption, bool keepsubscriberreferencealive, predicate<tpayload> filter);
1.action参数
其中action参数则是我们接受消息的函数
2.threadoption参数
threadoption类型参数threadoption是个枚举类型参数,代码如下:
threadoption.cs
public enum threadoption { /// <summary> /// the call is done on the same thread on which the <see cref="pubsubevent{tpayload}"/> was published. /// </summary> publisherthread, /// <summary> /// the call is done on the ui thread. /// </summary> uithread, /// <summary> /// the call is done asynchronously on a background thread. /// </summary> backgroundthread }
三种枚举值的作用:
- publisherthread:默认设置,使用此设置能接受发布者传递的消息
- uithread:可以在ui线程上接受事件
- backgroundthread:可以在线程池在异步接受事件
3.keepsubscriberreferencealive参数
默认keepsubscriberreferencealive为false,在prism官方是这么说的,该参数指示订阅使用弱引用还是强引用,false为弱引用,true为强引用:
- 设置为true,能够提升短时间发布多个事件的性能,但是要手动取消订阅事件,因为事件实例对保留对订阅者实例的强引用,否则就算窗体关闭,也不会进行gc回收.
- 设置为false,事件维护对订阅者实例的弱引用,当窗体关闭时,会自动取消订阅事件,也就是不用手动取消订阅事件
4.filter参数
filter是一个predicate
效果如下: 最后,附上整个demo的源代码:prismdemo源码
patientdetailviewmodel.cs: _ea.getevent<medicinesentevent>().subscribe(medicinemessagereceived,
threadoption.publisherthread,false,medicine=>medicine.name=="当归"|| medicine.name== "琼浆玉露");
五.源码
上一篇: jQuery---淘宝精品案例
下一篇: 去掉RAR右键解压菜单的VBS代码
推荐阅读
-
.NET Core 3 WPF MVVM框架 Prism系列之模块化
-
.NET Core 3 WPF MVVM框架 Prism系列之区域管理器
-
.NET Core 3 WPF MVVM框架 Prism系列之事件聚合器
-
.NET Core 3 WPF MVVM框架 Prism系列之对话框服务
-
.NET Core 3 WPF MVVM框架 Prism系列之导航系统
-
.NET Core 3 WPF MVVM框架 Prism系列之区域管理器
-
.NET Core 3 WPF MVVM框架 Prism系列之模块化
-
.NET Core 3 WPF MVVM框架 Prism系列之事件聚合器
-
.NET Core 3 WPF MVVM框架 Prism系列之导航系统
-
.NET Core 3 WPF MVVM框架 Prism系列之对话框服务