WPF MVVM 从Prism中学习设计模式之Event Aggregator 模式

Prism是由微软Patterns & Practices团队开发的项目,目的在于帮助开发人员构建松散耦合的、更灵活、更易于维护并且更易于测试的WPF应用或是Silverlight应用以及Windows Phone 7应用。使用Prism可以使程序开发更趋于模块化,整个项目将由多个离散的、松耦合的模块组成,而各个模块又可以又不同的开发者或团队进行开发、测试和部署。目前Prism的最新版本是Prism 4,于2010年11月12日发布。Prism有很完整的文档以及丰富的示例程序。在这里我们仅针对于Silverlight程序的开发。

个人理解以及应用

WPF MVVM模式时为了解决各个ViewModel之间频繁的互相调用或者委托调用。以及非MVVM模式下各个类库组件之间通讯重复引用问题。
因为开发需要,同时解决结构体之不进行重复引用,情况下进行交互,因此采用微软最新提供的订阅发布开源库进行开发。
发布者进行发布,不需要知道订阅者是谁,订阅者进行订阅不需要知道发布者是谁,通过 IEventAggregator 事件进行交互。
事件发布以后,所有订阅过的对象都收到通知。类似于WCF与客户端交互时的回调函数功能,服务器处理完成后,通过回调函数来通知客户端处理完成。
使用场景:

首先需要引用Microsoft.Practices.Prism.dll类库

实例代码

以发送消息通知为例,收到新消息后,要通知对应的消息接收方,用于接收到消息之后的处理

定义 消息通知时间 ,用于订阅和发布事件使用

  /// <summary>
    /// Event Aggregator Repository 保证同一个EventAggregate调用
    /// </summary>
    public class EventAggregatorRepository
    {
        public IEventAggregator EventAggregator;

        private static EventAggregatorRepository eventRespository = null;

        private EventAggregatorRepository()
        {
            EventAggregator = new EventAggregator();
        }

        public static EventAggregatorRepository GetInstance()
        {
            if (eventRespository == null)
            {
                eventRespository = new EventAggregatorRepository();
            }
            return eventRespository;
        }
    }

   /// <summary>
    /// Send Notice Event  string 是消息类型 可以是任意类型 object,string int
    /// </summary>
   public class SendNoticeEvent : CompositePresentationEvent<string>
    {
    }

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using Microsoft.Practices.Prism.Events;

namespace Sample
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SetSubscribe();
        }

        //  进行发布
        public void SetPublish(string messageData)
        {
            EventAggregatorRepository.GetInstance().EventAggregator
                             .GetEvent<SendNoticeEvent>()
                             .Publish(messageData);

        }


        // 订阅事件
        public void SetSubscribe()
        {
                        //订阅指定处理线程
                        // ThreadOption.UIThrea  页面线程处理
                         //ThreadOption.BackgroundThread 后台线程处理
                         //ThreadOption.PublisherThread 发布者线程处理

            EventAggregatorRepository.GetInstance().EventAggregator
                           .GetEvent<SendNoticeEvent>().Subscribe(ReceiveMessage, ThreadOption.UIThread, true);

        }


        // Receive Message Opration
        public void ReceiveMessage(string messageData)
        {
           
            this.textReceiveMessage.Text = messageData;
        }

        // Send Data Event
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SetPublish(this.textSendMessage.Text);
        }


    }
}

常见异常

Prism的EventAggregator的事件订阅错误

在prism的EventAggregator事件订阅会指向一个方法,而该方法不能为private,否则该事件订阅将会无法方法该方法导致订阅错误!正确做法是将指向的方法改为public

原文地址:https://www.cnblogs.com/HelloXZ/p/3384367.html