• 14-15事件聚合器


    声明

    原文出处如下:

    作者:RyzenAdorer 内容简介:Prism医疗案例

    作者:可是我爱你啊 内容简介:Prism官方案例的学习分享

    Prism官网开发文档

    容器相关知识的学习

    Microsoft官方文档

    这是一篇记录作者学习Prism的随笔,该随笔的内容是作者通过5个资源学习汇总而成,主要是为了方便自己以后拾遗温习所用,如果文中内容看不懂,推荐直接阅读相关原文。

    14-15事件聚合器

    IEventAggregator 事件聚合器

    1. 创建事件文件夹Events

    2. 创建事件,public class MessageSentEvent : PubSubEvent,继承 PubSubEvent接口,发布订阅接口

    3. 订阅事件, _ea.GetEvent().Subscribe(PatientMessageReceived);

    4. 发布事件,_ea.GetEvent().Publish(patient);

    5. 取消事件: _ea.GetEvent().Unsubscribe(MedicineMessageReceived);

    6. 过滤事件:

       _ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived, ThreadOption.PublisherThread, false, (filter) => filter.Contains("Brian"));
      

    订阅事件时过滤事件

    官方教程多发布多订阅

    作为订阅者,我想订阅特定类型的Payload,上个例子中我只想订阅message里含有Brian的事件,怎么处理呢?Prism为Subscribe方法实现了一个过滤器:

                _ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived, ThreadOption.PublisherThread, false, (filter) => filter.Contains("Brian"));
    

    过滤器是一个Predicate filter,参考 Predicate 委托

    博客案例多发布多订阅

    同理,我们实现搜索后的Medicine添加到当前病人列表中也是跟上面步骤一样,在Events文件夹创建事件类MedicineSentEvent:

    MedicineSentEvent.cs:

    Copy public class MedicineSentEvent: PubSubEvent<Medicine>
     {
    
     }
    

    在病人详细窗体的PatientDetailViewModel类订阅该事件:
    PatientDetailViewModel.cs:

    Copy 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:

    Copypublic 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:

    Copy 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);//发布消息
     }
    
     
    

    效果如下:
    img

    然后我们看看现在Demo项目的事件模型和程序集引用情况,如下图:

    img

    我们发现PatientModule和MedicineModule两个模块之间做到了通讯,但却不相互引用,依靠引用PrismMetroSample.Infrastructure程序集来实现间接依赖关系,实现了不同模块之间通讯且低耦合的情况

    四.几种订阅方式设置

    我们在Demo已经通过消息聚合器的事件机制,实现订阅者和发布者之间的通讯,我们再来看看,Prim都有哪些订阅方式,我们可以通过PubSubEvent类上面的Subscribe函数的其中最多参数的重载方法来说明:

    Subscribe.cs:

    Copypublic virtual SubscriptionToken Subscribe(Action<TPayload> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate<TPayload> filter);
    

    1.action参数

    其中action参数则是我们接受消息的函数

    2.threadOption参数

    ThreadOption类型参数threadOption是个枚举类型参数,代码如下:
    ThreadOption.cs

    Copypublic 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:

    Copy  _ea.GetEvent<MedicineSentEvent>().Subscribe(MedicineMessageReceived,
    ThreadOption.PublisherThread,false,medicine=>medicine.Name=="当归"|| medicine.Name== "琼浆玉露");
    

    效果如下:
    img

    五.源码

    最后,附上整个demo的源代码:PrismDemo源码

    作者: RyzenAdorer

    出处:https://www.cnblogs.com/ryzen/p/12196619.html

    登峰造极的成就源于自律
  • 相关阅读:
    4.8 C++ typeid操作符
    4.7 C++ dynamic_cast操作符
    tomcat中class和jar的加载顺序(转)
    java Files类和Paths类的用法 (转)
    搭建DUBBO项目解决DUBBO.XML标签报错的问题(转载)
    Maven异常:Could not find artifact
    在docker宿主机上查找指定容器内运行的所有进程的PID
    jmap、jstack、jps无法连接jvm解决办法
    linux中如何查看某一进程的启动时间
    Eureka与ZooKeeper 的比较(转)
  • 原文地址:https://www.cnblogs.com/fishpond816/p/13467427.html
Copyright © 2020-2023  润新知