prism WPF 消息的订阅或发布
EventMessage
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cjh.PrismWpf
{
/// <summary>
/// 事件消息
/// </summary>
public class EventMessage : PubSubEvent
{
}
/// <summary>
/// 事件消息 参数
/// </summary>
public class EventMessageArgs : PubSubEvent<object>
{
}
/// <summary>
/// 事件消息 参数,委托
/// </summary>
public class EventAction
{
/// <summary>
/// 结果委托
/// </summary>
public Action<bool> ResultAction { get; set; }
}
}
Messenger
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cjh.PrismWpf
{
/// <summary>
/// 全局静态 事件
/// </summary>
public class Messenger
{
/// <summary>
/// 全局静态 事件
/// </summary>
public static IEventAggregator Defualt { get; set; }
}
}
MainWindow
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Cjh.PrismWpf
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
//IEventAggregator 消息对象
public MainWindow(IEventAggregator eventAggregator)
{
InitializeComponent();
//this.DataContext = new MainViewModel();
//方式一
//1.获取消息对象,通过注入的方式获取
//2.通过消息对象进行订阅或发布
//总线 事件对象
// Subscribe 订阅
eventAggregator.GetEvent<EventMessage>().Subscribe(Receive);
//Publish 发布
eventAggregator.GetEvent<EventMessage>().Publish();
//方式二
//全局静态处理
//发布和订阅 注意先后顺序,先发布再订阅
Messenger.Defualt.GetEvent<EventMessageArgs>().Subscribe(Receive);
Messenger.Defualt.GetEvent<EventMessageArgs>().Publish("Hello");//有参
//发布委托消息后要一个返回结果
Messenger.Defualt.GetEvent<EventMessageArgs>().Publish(new EventAction()
{
ResultAction = new Action<bool>(state => {
//state 就是返回的结果
MessageBox.Show("执行结果:"+ state);
})
});
//订阅 增加过滤条件 arg => arg.Id == 1 是过滤条件
Messenger.Defualt.GetEvent<EventMessageArgsDataModel>().Subscribe(ReceiveDataModel, arg => arg.Id == 1);
Messenger.Defualt.GetEvent<EventMessageArgsDataModel>().Publish(new DataModel(){ Id = 1});
Messenger.Defualt.GetEvent<EventMessageArgsDataModel>().Subscribe(ReceiveDataModel2, arg => arg.Id == 2);
Messenger.Defualt.GetEvent<EventMessageArgsDataModel>().Publish(new DataModel() { Id = 2 });
//keepSubscriberReferenceAlive 为true时,对象销毁的时候,要做注销操作
//public SubscriptionToken Subscribe(Action<TPayload> action, bool keepSubscriberReferenceAlive)
Messenger.Defualt.GetEvent<EventMessageArgs>().Subscribe(Receive, true);// 在窗口关闭时做注销操作
//多线程状态控制
//public SubscriptionToken Subscribe(Action<TPayload> action, ThreadOption threadOption)
//关于ThreadOption选项
//PublisherThread :发布者在什么线程发布的,这个注册的逻辑就在哪个线程执行
//UIThread : 不管发布者在哪个线程发布,这里注册的逻辑总在UI线程执行,如果执行逻辑里有页面控件的操作,可以选择这个选项
//BackgroundThread : 不管发布者在哪个线程发布,这里注册的逻辑总在后台线程执行。(新建一个线程)如果执行逻辑与主线程无关,可以用这个
Messenger.Defualt.GetEvent<EventMessageArgs>().Subscribe(Receive, ThreadOption.PublisherThread);
}
/// <summary>
/// 订阅 接收消息
/// </summary>
private void Receive()
{
}
/// <summary>
/// 订阅 接收消息(有参数)
/// </summary>
private void Receive(object args)
{
MessageBox.Show(args.ToString());
}
/// <summary>
/// 订阅 接收消息(有参数)
/// </summary>
private void ReceiveDataModel(DataModel args)
{
MessageBox.Show(args.Id.ToString());
}
/// <summary>
/// 订阅 接收消息(有参数)
/// </summary>
private void ReceiveDataModel2(DataModel args)
{
MessageBox.Show(args.Id.ToString());
}
private void Window_Closed(object sender, EventArgs e)
{
//keepSubscriberReferenceAlive 为true时,对象销毁的时候,要做注销订阅操作
if (Messenger.Defualt.GetEvent<EventMessageArgs>().Contains(Receive))//判断是否存在订阅操作
{
Messenger.Defualt.GetEvent<EventMessageArgs>().Unsubscribe(Receive);//注销订阅
}
}
}
}
SubWindow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Cjh.PrismWpf
{
/// <summary>
/// SubWindow.xaml 的交互逻辑
/// </summary>
public partial class SubWindow : Window
{
public SubWindow()
{
InitializeComponent();
Messenger.Defualt.GetEvent<EventMessageArgs>().Subscribe(Receive);
}
private void Receive(object obj)
{
if (obj is EventAction)
{
//一些处理逻辑
//....................
var ea = obj as EventAction;
ea.ResultAction.Invoke(true);//true 是返回的值
}
}
}
}
App.xaml
using Prism.Events;
using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace Cjh.PrismWpf
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : PrismApplication
{
/// <summary>
/// 提供主窗口的对象
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
protected override Window CreateShell()
{
//单例 全局静态
Messenger.Defualt = Container.Resolve<IEventAggregator>();
new SubWindow().Show();
//return new MainWindow() { Title = "PrismMainWindow" };
return Container.Resolve<MainWindow>();
}
/// <summary>
/// 业务中所有需要注入的对象,在这个方法里注册
/// </summary>
/// <param name="containerRegistry"></param>
/// <exception cref="NotImplementedException"></exception>
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
}