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才能收到,否则收不到