IEventAggregator实现发布-订阅模式
IEventAggregator 是 Prism 框架中的一个核心接口,用于实现发布-订阅模式(Pub-Sub)。它允许不同的组件(如 ViewModel、Service 等)通过事件进行通信,而无需直接引用彼此,从而实现松耦合。
以下是实现 IEventAggregator 的步骤和示例代码:
1. 引入 Prism 库
确保项目中已经安装了 Prism 库。如果未安装,可以通过 NuGet 安装:
Install-Package Prism.Core
2. 定义事件类
事件类是用于传递数据的载体,通常继承自 PubSubEvent<T>,其中 T 是事件参数的类型。
using Prism.Events;
/// <summary>
/// 定义一个事件,用于传递字符串消息
/// </summary>
public class MessageSentEvent : PubSubEvent<string>
{
}
3. 发布事件
在需要发布事件的类中(如 ViewModel),通过 IEventAggregator 发布事件。
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
public class PublisherViewModel : BindableBase
{
private readonly IEventAggregator _eventAggregator;
private string _message;
public PublisherViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
SendMessageCommand = new DelegateCommand(SendMessage);
}
/// <summary>
/// 消息内容
/// </summary>
public string Message
{
get => _message;
set => SetProperty(ref _message, value);
}
/// <summary>
/// 发送消息命令
/// </summary>
public DelegateCommand SendMessageCommand { get; }
private void SendMessage()
{
// 发布事件
_eventAggregator.GetEvent<MessageSentEvent>().Publish(Message);
}
}
4. 订阅事件
在需要订阅事件的类中(如另一个 ViewModel),通过 IEventAggregator 订阅事件。
using Prism.Events;
using Prism.Mvvm;
public class SubscriberViewModel : BindableBase
{
private string _receivedMessage;
public SubscriberViewModel(IEventAggregator eventAggregator)
{
// 订阅事件
eventAggregator.GetEvent<MessageSentEvent>().Subscribe(OnMessageReceived);
}
/// <summary>
/// 接收到的消息
/// </summary>
public string ReceivedMessage
{
get => _receivedMessage;
set => SetProperty(ref _receivedMessage, value);
}
/// <summary>
/// 事件处理程序
/// </summary>
private void OnMessageReceived(string message)
{
ReceivedMessage = message;
}
}
5. 注册和解析依赖
在 Prism 的依赖注入容器中注册 IEventAggregator 和相关 ViewModel。
using Prism.Ioc;
using Prism.Modularity;
public class AppModule : IModule
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
// 注册 IEventAggregator
containerRegistry.RegisterSingleton<IEventAggregator, EventAggregator>();
// 注册 ViewModel
containerRegistry.Register<PublisherViewModel>();
containerRegistry.Register<SubscriberViewModel>();
}
public void OnInitialized(IContainerProvider containerProvider)
{
// 初始化逻辑(可选)
}
}
6. 在界面中绑定 ViewModel
在 XAML 或代码中绑定 ViewModel。
<Window x:Class="YourNamespace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<!-- 发布者 -->
<TextBox Text="{Binding Message, UpdateSourceTrigger=PropertyChanged}" Width="200" />
<Button Content="发送消息" Command="{Binding SendMessageCommand}" Margin="0,10,0,0" />
<!-- 订阅者 -->
<TextBlock Text="{Binding ReceivedMessage}" Margin="0,20,0,0" />
</StackPanel>
</Grid>
</Window>
7. 运行效果
- 在文本框中输入消息并点击“发送消息”按钮。
- 订阅者 ViewModel 会接收到消息并更新界面。
关键点说明
-
IEventAggregator:- 是 Prism 的事件聚合器接口,用于管理事件的发布和订阅。
- 通过
GetEvent<T>方法获取特定事件的实例。
-
PubSubEvent<T>:- 是 Prism 提供的事件基类,用于定义事件。
T是事件参数的类型。
-
发布事件:
- 使用
Publish方法发布事件。 - 事件参数会传递给所有订阅者。
- 使用
-
订阅事件:
- 使用
Subscribe方法订阅事件。 - 可以指定事件处理程序,并在事件触发时执行。
- 使用
-
依赖注入:
- 通过 Prism 的依赖注入容器注册和解析
IEventAggregator和 ViewModel。
- 通过 Prism 的依赖注入容器注册和解析
扩展功能
-
线程选项:
- 在订阅事件时,可以指定线程选项(如
ThreadOption.UIThread),确保事件处理程序在 UI 线程执行。
eventAggregator.GetEvent<MessageSentEvent>().Subscribe(OnMessageReceived, ThreadOption.UIThread); - 在订阅事件时,可以指定线程选项(如
-
过滤事件:
- 可以使用
Subscribe的重载方法过滤事件。
eventAggregator.GetEvent<MessageSentEvent>().Subscribe(OnMessageReceived, ThreadOption.UIThread, keepSubscriberReferenceAlive: true, filter: message => message.StartsWith("Important")); - 可以使用
-
取消订阅:
- 使用
Unsubscribe方法取消订阅事件。
var token = eventAggregator.GetEvent<MessageSentEvent>().Subscribe(OnMessageReceived); eventAggregator.GetEvent<MessageSentEvent>().Unsubscribe(token); - 使用
通过以上实现,可以轻松地在 Prism 应用中使用 IEventAggregator 实现组件间的松耦合通信。

浙公网安备 33010602011771号