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);
})
});
}
/// <summary>
/// 订阅 接收消息
/// </summary>
private void Receive()
{
}
/// <summary>
/// 订阅 接收消息(有参数)
/// </summary>
private void Receive(object args)
{
MessageBox.Show(args.ToString());
}
}
}
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)
{
}
}
}