添加一个事件
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModuleA.Event
{
public class MessageEvent: PubSubEvent<string>
{
}
}
ViewCViewModel
public class ViewCViewModel : IDialogAware
{
private readonly IEventAggregator aggregator;
public DelegateCommand CancleCommand { get; set; }
public DelegateCommand SaveCommand { get; set; }
public ViewCViewModel(IEventAggregator aggregator)
{
CancleCommand = new DelegateCommand(Cancle);
SaveCommand = new DelegateCommand(Save);
this.aggregator = aggregator;
}
构造函数里添加事件字段
private readonly IEventAggregator aggregator;
取消方法里发布事件
private void Cancle()
{
//使用MessageEvent 发布一个Hello
aggregator.GetEvent<MessageEvent>().Publish("Hello");
}
ViewC.xaml.cs
在ViewC里发布事件
using ModuleA.Event;
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 ModuleA.Views
{
/// <summary>
/// ViewC.xaml 的交互逻辑
/// </summary>
public partial class ViewC : UserControl
{
public ViewC(IEventAggregator aggregator)
{
InitializeComponent();
aggregator.GetEvent<MessageEvent>().Subscribe(arg =>
{
MessageBox.Show($"接收到消息:{ arg}");
});
}
}
}