Prism 基础知识学习(五)发布订阅

Prism 基础知识学习(五)发布订阅

在 ViewBViewModel.cs中

    public class ViewBViewModel : IDialogAware
    {
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="agregator"></param>
        public ViewBViewModel(IEventAggregator agregator)
        {
            agregator.GetEvent<MessageEvent>().Publish("Hellow");
        }

        public string Title { get; set; }


        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
        }

在 ViewC.xaml.cs中

    public partial class ViewC : UserControl
    {
        public ViewC(IEventAggregator eventAggregator)
        {
            InitializeComponent();
            eventAggregator.GetEvent<MessageEvent>().Subscribe(arg =>
                {
                    MessageBox.Show($"接收到消息:{arg}");
                }
                );
        }
    }

新建类 MessageEvent.cs

namespace ModuleB
{
    public class MessageEvent:PubSubEvent<string>
    {
    }
}
posted @ 2023-03-18 02:36  聆听微风  阅读(102)  评论(0编辑  收藏  举报