- 添加订阅按钮,发布按钮,取消订阅按钮
<StackPanel Grid.Column="2">
<Button Content="前进" Command="{Binding ForwardCommand}"></Button>
<Button Content="后退" Command="{Binding BackCommand}"></Button>
<Button Content="打开输入窗口" Command="{Binding OpenDialogCommand}"></Button>
<Button Content="发布事件" Command="{Binding PublishEventCommand}"></Button>
<Button Content="订阅事件" Command="{Binding SubcribeEventCommand}"></Button>
<Button Content="取消订阅事件" Command="{Binding UnSubcribeEventCommand}"></Button>
</StackPanel>
- 定义事件类型,注意消息内容
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp1.Events
{
public class MessageEvent:PubSubEvent<string>
{
}
}
- 订阅,取消订阅,发布事件
public class MainWindowViewModel:BindableBase
{
#region 私有字段区
private readonly IRegionManager _regionManager;
private readonly IDialogService _dialogService;
private readonly IEventAggregator _eventAggregator;
//接收到消息处理
private void OnReceive(string msg)
{
MessageBox.Show("接收到消息:" + msg);
}
//订阅事件
public ICommand SubcribeEventCommand
{
get => new DelegateCommand(() =>
{
_eventAggregator.GetEvent<MessageEvent>().Subscribe(OnReceive);
});
}
//取消事件订阅
public ICommand UnSubcribeEventCommand
{
get => new DelegateCommand(() =>
{
_eventAggregator.GetEvent<MessageEvent>().Unsubscribe(OnReceive);
});
}
//发布事件消息
public ICommand PublishEventCommand
{
get => new DelegateCommand(() =>
{
_eventAggregator.GetEvent<MessageEvent>().Publish("消息内容");
});
}
#endregion
public MainWindowViewModel(IRegionManager regionManager,IDialogService dialogService,IEventAggregator eventAggregator)
{
this._regionManager = regionManager;
this._dialogService = dialogService;
this._eventAggregator = eventAggregator;
}
}
- PubSubEvent和IEventAggregator 介绍
PubSubEvent
是Prism框架中的一个类,用于在不同的模块或组件之间实现发布-订阅(Publish-Subscribe)模式的事件通信。通过使用PubSubEvent
,可以实现解耦和松散耦合的组件间通信,使组件之间可以相互发布和订阅事件。
使用PubSubEvent
需要进行以下步骤:
- 定义事件类型:
首先,需要定义自己的事件类型。可以是一个普通的类,也可以是一个继承自PubSubEvent
的派生类。通常建议使用自定义的派生类,这样可以添加一些额外的信息或数据到事件中。
public class MyEvent : PubSubEvent<string>
{
}
在上述示例中,MyEvent
继承自PubSubEvent
,并指定了事件所携带的数据类型为string
。
- 发布事件:
发布事件即在某个地方触发事件,并将相关数据传递给订阅者。在需要发布事件的地方,通过IEventAggregator
接口的实例来获取相应的事件对象,并调用Publish
方法。
private readonly IEventAggregator _eventAggregator;
public MyPublisher(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
public void PublishEvent(string data)
{
_eventAggregator.GetEvent<MyEvent>().Publish(data);
}
在上述示例中,通过_eventAggregator
的GetEvent
方法来获取MyEvent
事件,并调用Publish
方法发布事件,并传递相应的数据。
- 订阅事件:
订阅事件即在某个组件或模块中注册事件的处理方法,当事件被发布时,这些方法将会被调用。订阅事件需要通过IEventAggregator
接口的实例来获取事件对象,并调用Subscribe
方法。
private readonly IEventAggregator _eventAggregator;
private readonly SubscriptionToken _subscriptionToken;
public MySubscriber(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_subscriptionToken = _eventAggregator.GetEvent<MyEvent>().Subscribe(OnEventReceived);
}
private void OnEventReceived(string data)
{
// 处理接收到的事件
Console.WriteLine(data);
}
public void UnsubscribeEvent()
{
_eventAggregator.GetEvent<MyEvent>().Unsubscribe(_subscriptionToken);
}
在上述示例中,通过_eventAggregator
的GetEvent
方法来获取MyEvent
事件,并调用Subscribe
方法注册事件处理方法OnEventReceived
。当事件发布时,OnEventReceived
方法将会被调用。
需要注意的是,订阅方法必须与发布事件时指定的数据类型相匹配。
通过使用PubSubEvent
,可以实现模块之间的解耦和松散耦合,提高代码的可维护性和扩展性。不同的模块可以独立发布和订阅事件,以实现模块间的通信和数据传递。