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 @   聆听微风  阅读(107)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示