.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为例子,更改PatientDetailViewModel订阅,代码如下:
PatientDetailViewModel.cs:
_ea.GetEvent<MedicineSentEvent>().Subscribe(MedicineMessageReceived,
ThreadOption.PublisherThread,false,medicine=>medicine.Name=="当归"|| medicine.Name== "琼浆玉露");
效果如下:

五.源码
最后,附上整个demo的源代码:PrismDemo源码
.NET Core 3 WPF MVVM框架 Prism系列之事件聚合器的更多相关文章
- C# prism 框架 MVVM框架 Prism系列之事件聚合器
网址:https://www.cnblogs.com/ryzen/p/12610249.html 本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的使用事件聚合器实现模块间的通信 ...
- .NET Core 3 WPF MVVM框架 Prism系列之区域管理器
本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的使用区域管理器对于View的管理 一.区域管理器 我们在之前的Prism系列构建了一个标准式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 Core3环境下使用MVVM框架Prism的命令的用法 一.创建DelegateCommand命令 我们在上一篇.NET Core 3 WPF MVVM框架 Prism系列之 ...
- .NET Core 3 WPF MVVM框架 Prism系列之对话框服务
本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的对话框服务,这也是prism系列的最后一篇完结文章,下面是Prism系列文章的索引: .NET Core 3 WPF MVVM框 ...
- .NET Core 3 WPF MVVM框架 Prism系列之模块化
本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的应用程序的模块化 前言 我们都知道,为了构成一个低耦合,高内聚的应用程序,我们会分层,拿一个WPF程序来说,我们通过MVVM模式 ...
- .NET Core 3 WPF MVVM框架 Prism系列之导航系统
本文将介绍如何在.NET Core3环境下使用MVVM框架Prism基于区域Region的导航系统 在讲解Prism导航系统之前,我们先来看看一个例子,我在之前的demo项目创建一个登录界面: 我们看 ...
- .NET Core 3 WPF MVVM框架 Prism系列之数据绑定
一.安装Prism 1.使用程序包管理控制台 Install-Package Prism.Unity -Version 7.2.0.1367 也可以去掉‘-Version 7.2.0.1367’获取最 ...
- Core 3 WPF MVVM框架 Prism系列之数据绑定
一.安装Prism 1.使用程序包管理控制台# Install-Package Prism.Unity -Version 7.2.0.1367 也可以去掉‘-Version 7.2.0.1367’获取 ...
随机推荐
- activiti工作流-概述
1 应用场景 1.1 业务流程 采购系统完成了企业的日常采购管理,包括采购单管理.采购单审核.入库.结算等模块. 用户角色包括:员工.部门经理.总经理.财务. 基本业务流程如下: 1.2 ...
- sublime text3神器插件
(首先安装)Package Control Package Control,就像 Linux 下的 apt-get 和 yum 一样,它是 Sublime Text 的包管理器 1.Emmet 是一 ...
- String、StringBuffer和StringBuild区别
String String是不可变对象,即对象一旦生成,就不能被更改.对String对象的改变会引发新的String对象的生成. String s = "abcd"; s = s+ ...
- P1919 FFT加速高精度乘法
P1919 FFT加速高精度乘法 传送门:https://www.luogu.org/problemnew/show/P1919 题意: 给出两个n位10进制整数x和y,你需要计算x*y. 题解: 对 ...
- 北京师范大学第十七届程序设计竞赛决赛 G
传送门:https://ac.nowcoder.com/acm/contest/895/G 题意: \[ 操作 1:L,R,X,Y,对所有L≤i≤R赋值 \\ Ai=min(Ai,(i−L)×Y+X) ...
- STVD、IAR两种编译器比较
1.全局查找功能: STVD:全局查找功能全局查找功能比较麻烦,有3个动作. IAR:有全局查找功能比较方便,只要一个动作,和KEIL一样. 2.编译时间 STVD:相对比较慢. IAR:相对快点. ...
- Hibernate映射文件详解(News***.hbm.xml)二
转自 http://blog.csdn.net/a9529lty/article/details/6454924 一.hibernate映射文件的作用: Hibernate映射文件是Hibernate ...
- HTML基础常识
什么是HTML? 超文本标记语言,用来制作网页 浏览器常识: 常见浏览器: 谷歌(Chrome).苹果(Safari) . IE(Edge).欧朋(Opera).火狐(Firefox) 浏览器内核:浏 ...
- Javascript事件系统
本文内容 事件基础 事件监听方式 事件默认行为 事件冒泡与事件捕获 事件绑定与事件委托 事件基础 注意:本文不会深入探究Javascript的事件循环. 提到事件,相信每位Javascript开发者都 ...
- 如何使用JMX来管理程序?
什么是JMX JMX,全称Java Management Extensions,用于我们管理和监控java应用程序.JMX有以下用途: 监控应用程序的运行状态和相关统计信息. 修改应用程序的配置(无需 ...