1 根据《prism 搭建项目》搭建Prism项目

2 新建类库项目Prism.UseEventAggregator,创建MessageSentEvent类,使其继承于PubSubEvent<string>

using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Prism.UseEventAggregator.Core
{public class MessageSentEvent:PubSubEvent<string>{}
}

3 新建wpf用户控件库ModuleA,通过nuget引入prism框架,在其下面创建Views目录,ViewModels目录

4 在Views目录下新建用户控件ViewA,在xaml中引入prism库,并使其自动关联到ViewModel

<UserControl x:Class="ModuleA.Views.ViewA"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:ModuleA.Views"xmlns:prism="http://prismlibrary.com/"prism:ViewModelLocator.AutoWireViewModel="True"Padding="25"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"><StackPanel><TextBox Text="{Binding Message}" Margin="5"/><Button Content="Send Message" Command="{Binding SendMessageCommand}" Margin="5"/></StackPanel>
</UserControl>

5 在ViewModels文件下创建类ViewAViewModel,注入IEventAggregator,当执行Command时候,通过事件聚合器拿到MessageSentEvent实例,并发布

using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.UseEventAggregator.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ModuleA.ViewModels
{public class ViewAViewModel:BindableBase{IEventAggregator eventAggregator;private string _Message;public string Message{get { return _Message; }set { _Message = value; }}public DelegateCommand SendMessageCommand { get; }public ViewAViewModel(IEventAggregator eventAggregator){this.eventAggregator = eventAggregator;SendMessageCommand = new DelegateCommand(SendMessage);}private void SendMessage(){eventAggregator.GetEvent<MessageSentEvent>().Publish(Message);}}
}

6 在ModuleA中新建ModuleAEntity类,使其继承于IModule

using ModuleA.Views;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ModuleA
{public class ModuleAEntity : IModule{public void OnInitialized(IContainerProvider containerProvider){var regionManager = containerProvider.Resolve<IRegionManager>();regionManager.RegisterViewWithRegion("LeftRegion", typeof(ViewA));}public void RegisterTypes(IContainerRegistry containerRegistry){}}
}

7 新建另一个wpf用户控件项目ModuleB,通过nuget引入prism框架,在其下面创建Views目录,ViewModels目录

8  在Views目录下新建用户控件ViewB,在xaml中引入prism库,并使其自动关联到ViewModel

<UserControl x:Class="ModuleB.Views.ViewB"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:ModuleB.Views"xmlns:prism="http://prismlibrary.com/"prism:ViewModelLocator.AutoWireViewModel="True"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" Padding="25"><Grid><ListBox ItemsSource="{Binding Messages}"/></Grid>
</UserControl>

9 在ViewModels文件下创建类ViewBViewModel,注入IEventAggregator,当执行Command时候,通过事件聚合器拿到MessageSentEvent实例,并订阅

using Prism.Events;
using Prism.Mvvm;
using Prism.UseEventAggregator.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ModuleB.ViewModels
{public class ViewBViewModel:BindableBase{IEventAggregator eventAggregator;private ObservableCollection<string> _Messages = new ObservableCollection<string>();public ObservableCollection<string> Messages{get { return _Messages; }set { SetProperty(ref _Messages, value); }}public ViewBViewModel(IEventAggregator eventAggregator){this.eventAggregator = eventAggregator;this.eventAggregator.GetEvent<MessageSentEvent>().Subscribe(MessageReceived);}private void MessageReceived(string obj){Messages.Add(obj);}}
}

10 在ModuleB中新建ModuleBEntity类,使其继承于IModule

using ModuleB.Views;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ModuleB
{public class ModuleBEntity : IModule{public void OnInitialized(IContainerProvider containerProvider){var regionManager = containerProvider.Resolve<IRegionManager>();regionManager.RegisterViewWithRegion("RightRegion", typeof(ViewB));}public void RegisterTypes(IContainerRegistry containerRegistry){}}
}

11 在MainWindow.xaml中定义两个Region

<Window x:Class="Prism.UseEventAggregator.Views.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Prism.UseEventAggregator"xmlns:prism="http://prismlibrary.com/"prism:ViewModelLocator.AutoWireViewModel="True"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition /></Grid.ColumnDefinitions><ContentControl prism:RegionManager.RegionName="LeftRegion" /><ContentControl Grid.Column="1" prism:RegionManager.RegionName="RightRegion" /></Grid>
</Window>

12 在App里面重写下面方法

  protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog){moduleCatalog.AddModule<ModuleAEntity>();moduleCatalog.AddModule<ModuleBEntity>();}

14 运行

15 修改ViewBViewModel添加接收事件过滤

public ViewBViewModel(IEventAggregator eventAggregator){this.eventAggregator = eventAggregator;this.eventAggregator.GetEvent<MessageSentEvent>().Subscribe(MessageReceived,ThreadOption.PublisherThread,true,(s)=>s.Contains("hi"));}

16 此时ModuleA只有发送hi开头的消息,ModuleB才能收到,否则收不到

prism EventAggregator(事件聚合器)相关推荐

  1. 从PRISM开始学WPF(七)MVVM(三)事件聚合器EventAggregator?

    从PRISM开始学WPF(七)MVVM(三)事件聚合器EventAggregator? 原文:从PRISM开始学WPF(七)MVVM(三)事件聚合器EventAggregator? 从PRISM开始学 ...

  2. 【Prism 8】事件聚合器(Event Aggregator)

    步骤: 继承PubSubEvent<T>创建自定义事件类型 创建事件发送者 创建事件接收者,并订阅指定的事件 1. 事件聚合器 Prism 提供了一种机制,可以实现应用程序中松散耦合组件之 ...

  3. .NET Core 3 WPF MVVM框架 Prism系列之事件聚合器

    本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的使用事件聚合器实现模块间的通信 一.事件聚合器  在上一篇 .NET Core 3 WPF MVVM框架 Prism系列之模块化 ...

  4. c#事件的发布-订阅模型_NET Core 3 WPF MVVM框架 Prism系列之事件聚合器

    本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的使用事件聚合器实现模块间的通信 一.事件聚合器#  在上一篇 .NET Core 3 WPF MVVM框架 Prism系列之模块化 ...

  5. 事件聚合器 - Caliburn.Micro 文档系列

    文章目录 事件聚合器 (Event Aggregator) 入门 创建与生命周期 发布事件 使用自定义线程发布事件 订阅事件 订阅许多事件 多态的订阅者 查询处理程序 协同感知订阅者 任务感知订阅者 ...

  6. Caliburn.Micro使用事件聚合器

    Caliburn.Micro框架使用观察者模式实现了事件聚合器 Caliburn.Micro对参数采用强类型的方式,相比MvvmLight算是省心,易用 详细的说明参见官方文档https://cali ...

  7. 0619-EventAggregator 事件聚合器 INotifyCollectionChanged DataTemplate ControlTemplate Region binding

    EventAggregator是Prism中专门处理ViewModel与ViewModel之间事件传递的类对象,它提供了针对事件的发布方法和订阅方法,所以可以非常方便的来管理事件.下面的图就是其实现的 ...

  8. 9个最佳新闻聚合器网站(+如何构建自己的网站)

    Do you want to read the latest news and updates from your favorite blogs all at one place? 您是否想一站式阅读 ...

  9. OpenTsdb官方文档中文版----聚合器

    OpenTSDB旨在在查询执行的过程中有效地组合多个不同的时间序列.原因在于:当用户查看他们的数据,他们通常会从高层的角度开始提问,例如"数据中心的总吞吐量是多少"或"当 ...

最新文章

  1. slide简介(大数据技术)
  2. 搭建VS2008+OpenCV2.1开发环境
  3. 阿里P8架构师谈:高并发网站的监控系统选型、比较、核心监控指标
  4. php mysql上传多张图片_PHP实现一次性多张图片上传功能
  5. JS concat() 方法
  6. Linux 小知识翻译 - 目录 (完结)
  7. mid=(left+right)1什么含义
  8. 机器学习与物理科学(四)(Machine learning and the physical sciences)
  9. 开博尔android播放器,你看!你细看!开博尔Q50播放器本地和NAS文件管理方式详解...
  10. gst-launch的-v参数
  11. wps工资表怎么用计算机,wps制作工资表的方法步骤图
  12. Inception(盗梦空间)及代码实现
  13. 五一节后收心大法,一个便签让满血复活
  14. 蘑菇街HR问我有什么缺点,我的回答是我没有缺点!
  15. html dashed显示实线怎么改,实现css虚线样式的两种方式:dotted和dashed(实例)
  16. mysql 错误 #2054 解决方案
  17. Camera效果测试-色彩准确性及饱和度测试
  18. [UE4笔记] 根据日期判断该天是周几
  19. win10环境下Android SDK下载安装及配置教程----Android SDK安装
  20. Nvidia Jetson Agx Xavier 在Ros中调用GMSL2相机

热门文章

  1. Apple Watch如何重新配对
  2. 计蒜客 幼儿园买玩具
  3. 如何设置ddns动态域名实现内网发布外网
  4. iOS-自己定义键盘选择器
  5. win10系统启动VMware虚拟机就蓝屏
  6. 火星人敏捷开发手册免费培训 By 火星人陈勇
  7. css-doodle初认识
  8. UE4 network优化
  9. 亚马逊FBA卖家最赚钱的产品是什么?
  10. { errcode: 88, sub_code: “40000“, sub_msg: “access_token is blank“, errmsg: “ding talk error[subcode