4. Prism系列之事件
Prism系列之事件
前言
通过上篇文章,我们留下了一些问题。
怎么处理同模块不同窗体之间的通信和不同模块之间不同窗体的通信?
Prism提供了一种事件机制,可以在应用程序中低耦合的模块之间进行通信,该机制基于事件聚合器服务,允许发布者和订阅者之间通过事件进行通讯,且彼此之间没有之间引用,这就实现了模块之间低耦合的通信方式。
下面引用官方的一个事件聚合器模型图:
一、创建和发布事件
1. 创建事件
首先我们来处理同模块不同窗体之间的通讯,我们在PrismMetroSample.Infrastructure
新建一个文件夹Events
,然后新建一个类PatientSentEvent
,代码如下:
PatientSentEvent.cs:
using Prism.Events;
using PrismMetroSample.Infrastructure.Models;
namespace PrismMetroSample.Infrastructure.Events
{
public class PatientSentEvent: PubSubEvent<Patient>
{
}
}
2. 订阅事件
在病人详细窗体的PatientDetailViewModel类订阅该事件,代码如下:
PatientDetailViewModel.cs:
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using PrismMetroSample.Infrastructure.Events;
using PrismMetroSample.Infrastructure.Models;
using PrismMetroSample.Infrastructure.Services;
using System.Collections.ObjectModel;
using System.Linq;
namespace PrismMetroSample.PatientModule.ViewModels
{
public class PatientDetailViewModel : BindableBase
{
#region Fields
private readonly IMedicineSerivce _medicineSerivce;
private readonly IEventAggregator _ea;
#endregion
#region Properties
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); }
}
#endregion
#region Commands
private DelegateCommand _cancleSubscribeCommand;
public DelegateCommand CancleSubscribeCommand =>
_cancleSubscribeCommand ?? (_cancleSubscribeCommand = new DelegateCommand(ExecuteCancleSubscribeCommand));
#endregion
#region Excutes
void ExecuteCancleSubscribeCommand()
{
_ea.GetEvent<MedicineSentEvent>().Unsubscribe(MedicineMessageReceived);
}
#endregion
public PatientDetailViewModel(IEventAggregator ea, IMedicineSerivce medicineSerivce)
{
_medicineSerivce = medicineSerivce;
_ea = ea;
_ea.GetEvent<PatientSentEvent>().Subscribe(PatientMessageReceived);
_ea.GetEvent<MedicineSentEvent>().Subscribe(MedicineMessageReceived,ThreadOption.PublisherThread,false,
medicine=>medicine.Name=="当归"|| medicine.Name== "琼浆玉露");
}
/// <summary>
/// 接受事件消息函数
/// </summary>
private void MedicineMessageReceived(Medicine medicine)
{
this.lstMedicines?.Add(medicine);
}
private void PatientMessageReceived(Patient patient)
{
this.CurrentPatient = patient;
this.lstMedicines = new ObservableCollection<Medicine>(_medicineSerivce.GetRecipesByPatientId(this.CurrentPatient.Id).FirstOrDefault().LstMedicines);
}
}
}
3. 发布消息
然后我们在病人列表窗体的PatientListViewModel
中发布消息,代码如下:
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using PrismMetroSample.Infrastructure;
using PrismMetroSample.Infrastructure.Constants;
using PrismMetroSample.Infrastructure.Events;
using PrismMetroSample.Infrastructure.Models;
using PrismMetroSample.Infrastructure.Services;
using PrismMetroSample.PatientModule.Views;
using System.Collections.Generic;
namespace PrismMetroSample.PatientModule.ViewModels
{
public class PatientListViewModel : BindableBase, IRegionMemberLifetime
{
#region Fields
private readonly IEventAggregator _ea;
private readonly IRegionManager _regionManager;
private readonly IPatientService _patientService;
private IRegion _region;
private PatientList _patientListView;
#endregion
#region Properties
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); }
}
public bool KeepAlive => true;
#endregion
#region Commands
private DelegateCommand<Patient> _mouseDoubleClickCommand;
public DelegateCommand<Patient> MouseDoubleClickCommand =>
_mouseDoubleClickCommand ?? (_mouseDoubleClickCommand = new DelegateCommand<Patient>(ExecuteMouseDoubleClickCommand));
#endregion
#region Excutes
/// <summary>
/// DataGrid 双击按钮命令方法
/// </summary>
void ExecuteMouseDoubleClickCommand(Patient patient)
{
this.ApplicationCommands.ShowCommand.Execute(FlyoutNames.PatientDetailFlyout);//打开窗体
_ea.GetEvent<PatientSentEvent>().Publish(patient);//发布消息
}
#endregion
/// <summary>
/// 构造函数
/// </summary>
public PatientListViewModel(IPatientService patientService, IEventAggregator ea, IApplicationCommands applicationCommands)
{
_ea = ea;
this.ApplicationCommands = applicationCommands;
_patientService = patientService;
this.AllPatients = _patientService.GetAllPatients();
}
}
}
效果如下:
4. 实现多订阅多发布
在Events
文件夹创建事件类MedicineSentEvent
:
MedicineSentEvent.cs:
using Prism.Events;
using PrismMetroSample.Infrastructure.Models;
namespace PrismMetroSample.Infrastructure.Events
{
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);//发布消息
}
效果如下:
我们看看现在项目的事件模型和程序集引用情况,如下图:
我们发现PatientModule
和MedicineModule
两个模块之间做到了通讯,但却不相互引用,依靠引用PrismMetroSample.Infrastructure
程序集来实现间接依赖关系,实现了不同模块之间通讯且低耦合的情况。
二、取消订阅事件
Prism
还提供了取消订阅的功能,我们在病人详细窗体提供该功能,PatientDetailViewModel
加上这几句:
private DelegateCommand _cancleSubscribeCommand;
public DelegateCommand CancleSubscribeCommand =>
_cancleSubscribeCommand ?? (_cancleSubscribeCommand = new DelegateCommand(ExecuteCancleSubscribeCommand));
void ExecuteCancleSubscribeCommand()
{
_ea.GetEvent<MedicineSentEvent>().Unsubscribe(MedicineMessageReceived);
}
三、订阅方式设置
我们已经通过消息聚合器的事件机制,实现订阅者和发布者之间的通讯,我们再来看看,Prism
都有哪些订阅方式,我们可以通过PubSubEvent
类上面的Subscribe
函数的其中最多参数的重载方法来说明:
Subscribe.cs:
public virtual SubscriptionToken Subscribe(Action<TPayload> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate<TPayload> filter);
action参数
action参数是我们接受消息的函数
threadOption参数
ThreadOption
类型参数threadOption
是个枚举类型参数,代码如下:
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
:可以在线程池在异步接受事件
keepSubscriberReferenceAlive参数
默认keepSubscriberReferenceAlive
为false,在Prism
官方是这么说的,该参数指示订阅使用弱引用还是强引用,false为弱引用,true为强引用:
- 设置为
true
,能够提升短时间发布多个事件的性能,但是要手动取消订阅事件,因为事件实例对保留对订阅者实例的强引用,否则就算窗体关闭,也不会进行GC回收. - 设置为
false
,事件维护对订阅者实例的弱引用,当窗体关闭时,会自动取消订阅事件,也就是不用手动取消订阅事件
filter参数
filter是一个Predicate的泛型委托参数,返回值为布尔值,可用来订阅过滤,以我们demo为例子,更改PatientDetailViewModel订阅,代码如下:
_ea.GetEvent<MedicineSentEvent>().Subscribe(MedicineMessageReceived,
ThreadOption.PublisherThread,false,medicine=>medicine.Name=="当归"|| medicine.Name== "琼浆玉露");
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!