Prism for WPF 第一讲 Event机制
在本篇文章中主要讲解在Prism中模块与模块之间事件关联的机制。在这里牵涉到三个名词:事件定义,事件发布,事件订阅。
第一:事件定义
在公共类库中定义事件。
①没有参数事件
public class NullClass { }
public class NullableEvent : CompositePresentationEvent<NullClass> { }
②简单类型参数事件
public class MessageAddedEvent : CompositePresentationEvent<string> { }
③复合类型参数事件
public class News
{
/// <summary>
/// 标题。
/// </summary>
public string Title { get; set; }
/// <summary>
/// 内容。
/// </summary>
public string Content { get; set; }
}
public class NewsAddedEvent : CompositePresentationEvent<News> { }
第二:事件发布(以下为示例代码)
using Microsoft.Practices.Composite.Events;
using PrismEvent.Infrastructure;
using System.Windows;
using System.Windows.Controls; namespace PrismEvent.Publisher
{
/// <summary>
/// PublisherView.xaml 的交互逻辑
/// </summary>
public partial class PublisherView : UserControl
{
IEventAggregator eventAggregator;
/// <summary>
/// 构造函数。
/// </summary>
public PublisherView()
{
InitializeComponent();
}
/// <summary>
/// 构造函数。
/// </summary>
/// <param name="eventAggregator">事件聚合。</param>
public PublisherView(IEventAggregator eventAggregator)
: this()
{
this.eventAggregator = eventAggregator;
}
/// <summary>
/// 简单参数。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSimpleParam_Click(object sender, RoutedEventArgs e)
{
string message = "Hellow world";
eventAggregator.GetEvent<MessageAddedEvent>().Publish(message);
}
/// <summary>
/// 复合参数。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCompositeParam_Click(object sender, RoutedEventArgs e)
{
News news = new News()
{
Title = "Bao's demo was published.",
Content = "This message is wonderful."
};
eventAggregator.GetEvent<NewsAddedEvent>().Publish(news);
} private void btnWithoutParam_Click(object sender, RoutedEventArgs e)
{
eventAggregator.GetEvent<NullableEvent>().Publish(null);
} private void btnStandard_Click(object sender, RoutedEventArgs e)
{
string message = "Open seasame!";
eventAggregator.GetEvent<StandardMessageAddedEvent>().Publish(message);
}
}
}
第三:事件订阅
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Events;
using PrismEvent.Infrastructure;
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 PrismEvent.Subscriber
{
/// <summary>
/// Subscriberview.xaml 的交互逻辑
/// </summary>
public partial class Subscriberview : UserControl
{
IEventAggregator eventAggregator;
private SubscriptionToken subscriptionToken;
public Subscriberview()
{
InitializeComponent();
} public Subscriberview(IEventAggregator eventAggregator)
: this()
{
this.eventAggregator = eventAggregator;
this.eventAggregator.GetEvent<MessageAddedEvent>().Subscribe(ShowMessage);
this.eventAggregator.GetEvent<NewsAddedEvent>().Subscribe(ShowMessage);
this.eventAggregator.GetEvent<NullableEvent>().Subscribe(DoSomething);
StandardMessageAddedEvent messageAddedEvent = eventAggregator.GetEvent<StandardMessageAddedEvent>();
if (subscriptionToken != null)
{
messageAddedEvent.Unsubscribe(subscriptionToken);
}
subscriptionToken = messageAddedEvent.Subscribe(ShowMessage2, ThreadOption.UIThread, true, Filter);
} private bool Filter(string message)
{
return true;
} private void ShowMessage2(string message)
{
this.tbSimpleParam.Text = message;
StandardMessageAddedEvent messageAddedEvent = eventAggregator.GetEvent<StandardMessageAddedEvent>();
if (subscriptionToken != null)
{
messageAddedEvent.Unsubscribe(subscriptionToken);
}
} private void DoSomething(NullClass obj)
{ }
/// <summary>
/// 显示新闻消息。
/// </summary>
/// <param name="news">新闻对象。</param>
private void ShowMessage(News news)
{
this.tbSimpleParam.Text = news.Title;
}
/// <summary>
/// 显示消息。
/// </summary>
/// <param name="message">消息内容。</param>
private void ShowMessage(string message)
{
this.tbSimpleParam.Text = message;
} }
}
对于事件的订阅,正规的写法如下所示:
StandardMessageAddedEvent messageAddedEvent = eventAggregator.GetEvent<StandardMessageAddedEvent>();
if (subscriptionToken != null)
{
messageAddedEvent.Unsubscribe(subscriptionToken);
}
subscriptionToken = messageAddedEvent.Subscribe(ShowMessage2, ThreadOption.UIThread, true, Filter);
在上面的代码中,我们看到这样的顺序:
1.先从IEventAggregator中获取注册到其中的事件对象,也就是messageAddedEvent;
StandardMessageAddedEvent messageAddedEvent=eventAggregator.GetEvent<StandardMessageAddedEvent>();
2.这个messageAddedEvent对象具有Subscribe方法,它返回一个SubscriptionToken类型的对象,用来标志事件触发后,在订阅一方所调用的方法:
subscriptionToken=messageAddedEvent.Subscribe(ShowMessage2,ThreadOption.UIThread,true,Filter);
3.在第2步之前,我们需要检查SubscriptionToken对象是否为空,否则就要注销之前的messageAddedEventHandler方法。
if(subscriptionToken!=null)
{
messageAddedEvnet.Unsubscribe(subscriptionToken);
}
之所以这么写,是因为系统弱引用后的垃圾自动回收时间,于是,将Subscribe方法的第三个参数设置为true(默认为false,弱引用,垃圾自动回收)。这样,订阅的就是强引用了,Prism就会要求我们必须手动取消事件的订阅。
在Subscribe方法中,第二个参数是ThreadOption枚举。值有:
publisherThread //在publisherThread所在线程上执行,默认值。
UIThread //在UI线程上触发。
BackgroundThread //在后台线程上异步调用。
第四个参数是一个bool类型的委托,它以事件传递的消息类型作为参数。
subscriptionToken=messageAddedEvent.Subscribe(ShowMessage2,ThreadOption.UIThread,true,Filter);
public bool Filter(string text){
return true;
}
注意:Filter方法总会在ShowMessage2方法前执行。如果返回true,则执行ShowMessage2。若为false则不执行。
在此,Prism的事件机制告一段落。
Prism for WPF 第一讲 Event机制的更多相关文章
- Prism for WPF再探(基于Prism事件的模块间通信)
上篇博文链接 Prism for WPF初探(构建简单的模块化开发框架) 一.简单介绍: 在上一篇博文中初步搭建了Prism框架的各个模块,但那只是搭建了一个空壳,里面的内容基本是空的,在这一篇我将实 ...
- Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天、消息模块
原文:Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天.消息模块 中秋节假期没事继续搞了搞 做了各聊天的模块,需要继续优化 第一步画页面 页面参考https://github.c ...
- Prism for WPF
Prism for WPF Prism for WPF初探(构建简单的模块化开发框架) 先简单的介绍一下Prism框架,引用微软官方的解释: Prism provides guidance to ...
- Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token
原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf? 因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...
- Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单
原文:Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单 昨天晚上把TreeView的样式做了一下,今天给TreeView绑了数据,实现了切换页面功能 上 ...
- 好玩的WPF第一弹:窗口抖动+边框阴影效果+倒计时显示文字
原文:好玩的WPF第一弹:窗口抖动+边框阴影效果+倒计时显示文字 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csd ...
- 干货|漫画算法:LRU从实现到应用层层剖析(第一讲)
今天为大家分享很出名的LRU算法,第一讲共包括4节. LRU概述 LRU使用 LRU实现 Redis近LRU概述 第一部分:LRU概述 LRU是Least Recently Used的缩写,译为最近最 ...
- CS193P - 2016年秋 第一讲 课程简介
Stanford 的 CS193P 课程可能是最好的 ios 入门开发视频了.iOS 更新很快,这个课程的最新内容也通常是一年以内发布的. 最新的课程发布于2016年春季.目前可以通过 iTunes ...
- POI教程之第一讲:创建新工作簿, Sheet 页,创建单元格
第一讲 Poi 简介 Apache POI 是Apache 软件基金会的开放源码函数库,Poi提供API给java程序对Microsoft Office格式档案读和写的功能. 1.创建新工作簿,并给工 ...
随机推荐
- VMWare虚拟机USB连接问题
错误31:连接到系统上的设备没有发挥作用 原文链接 描述 常用 VMware 虚拟机的有事应该遇到这种情况,就是装完 VMware ,启动时 VMware 下面会有个黄框中有" USB di ...
- Spring-demo1(初学者的尝试,2015.03.19)
项目结构: 源代码如下: package com.bean; public interface Person { public void Speak(); } package com.bean; pu ...
- HW2.24
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- POJ3349: Snowflake Snow Snowflakes(hash 表)
考察hash表: 每一个雪花都有各自的6个arm值,如果两个雪花从相同或者不同位置开始顺时针数或者逆时针数可以匹配上,那么这两个雪花就是相等的. 我们采用hash的方法,这样每次查询用时为O(1),总 ...
- SECURITY_ATTRIBUTES 设置低权限
Windows 从 Vista 開始又一次改动了其系统的权限管理机制,于是如今就会碰到一些 xp 能过而 win7 不能过的代码.比方 Service 程序和一般应用程序用共享内存的方式来通讯,Cre ...
- PreferenceActivity 自动保存属性
package com.example.preference; import android.content.Context; import android.os.Bundle; import and ...
- Unicode编码及其实现:UTF-16、UTF-8,and more
http://blog.csdn.net/thl789/article/details/7506133
- InnoDB的redo日志管理---饶珑辉
http://raolonghui.com/2015/06/24/innodb%E7%9A%84redo%E6%97%A5%E5%BF%97%E7%AE%A1%E7%90%86/#comment-11
- 【转】GitHub平台最火的iOS开源项目——2013-08-25 17
http://www.cnblogs.com/lhming/category/391396.html 今天,我们将介绍20个在GitHub上非常受开发者欢迎的iOS开源项目,你准备好了吗? 1. AF ...
- NotificationListenerService不能监听到通知
作者:Hugo链接:https://www.zhihu.com/question/33540416/answer/113706620来源:知乎著作权归作者所有,转载请联系作者获得授权. 背景知识: 所 ...