博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【学习笔记】Silverlight框架:Jounce(4)——事件通信
阅读量:4640 次
发布时间:2019-06-09

本文共 2331 字,大约阅读时间需要 7 分钟。

Prism、CM和Jounce里都有各自的事件通信机制,也都叫EventAggregator。

相比于Prism,Jounce里的EventAggregator的风格更接近CM。当然作者也是这么说的:The pattern here is based on the lightweight version Rob Eisenburg introduced with Caliburn Micro。

这里涉及的类主要有三个:两个接口类IEventAggregator和IEventSink<in T>,还有实现IEventAggregator的EventAggregatorService。

IEventAggregator定义了四个方法,其中三个是订阅消息、在UI线程上订阅消息和取消订阅,接受的参数都是IEventSink;最后一个方法是用来发布消息的。

EventAggregatorService内部维护了一个弱引用的字典用来存储订阅了消息的IEventSink,当在某个地方谁谁谁发布消息的时候,就会在这个字典里去找泛型类型匹配的IEventSink并调用其唯一的一个方法HandleEvent。

其实就是观察者,你懂的。

来实例操作下,通过模板新建解决方案,整个结构如下:

MainPage分为左右2个部分,分别用来存放PublishIntSubscribeGuid和PublishGuidSubscribeInt。

PublishIntSubscribeGuid会发布Int值,然后接收Guid的消息。

PublishGuidSubscribeInt会发布Guid,然后接收Int值的消息。

两个页面都是上面一个按钮,下面一个TextBox用来显示收到的消息,后台代码也差不多,我们来看看其中一个:

[Export]    public partial class PublishIntSubscribeGuid : UserControl, IEventSink
, IPartImportsSatisfiedNotification { [Import] public IEventAggregator EventAggregator { get; set; } public PublishIntSubscribeGuid() { InitializeComponent(); this.PublishButton.Click += new RoutedEventHandler(PublishButton_Click); } void PublishButton_Click(object sender, RoutedEventArgs e) { var random = new Random(); this.EventAggregator.Publish(random.Next()); } #region IPartImportsSatisfiedNotification 成员 public void OnImportsSatisfied() { EventAggregator.SubscribeOnDispatcher(this); } #endregion #region IEventSink
成员 public void HandleEvent(Guid publishedEvent) { this.OutTextBox.Text += "\r\n" + publishedEvent; } #endregion }

首先,要订阅Guid类型的消息先要实现IEventSink接口,泛型类型是Guid。

然后,要实现IPartImportsSatisfiedNotification接口,在MEF导入IEventAggregator后,注册完成消息订阅(这里在UI线程上订阅),这样当有某个地方发布类型是GUID的消息时,HandleEvent(Guid publishedEvent)方法就会最终被调用。

另外,在点击发布消息的按钮时候发布个随机的Int值,当某个地方订阅了Int类型的消息时,那个地方就会接收到这个随机值。

实际应用可能要复杂点,泛型会是某个实体类或者某种错误等等。

看一下效果:

代码:

PS:

1.打开APP.CS,可以看到通过模板创建的解决方案默认使APP实现了 IEventSink<UnhandledExceptionEvent>接口,这个就是原来通过Application.Current.UnhandledException注册的系统未处理错误,Jounce在里进行了注册和转换并通过事件系统进行了发布。这样就可以在别的地方也进行侦听。

2.整个事件通信系统是Jounce页面加载和导航的基础,可以实现IEventSink<ViewNavigationArgs>并侦听下看看。

转载于:https://www.cnblogs.com/HalfwayMonk/archive/2011/07/28/2120465.html

你可能感兴趣的文章
CSS选择器总结
查看>>
mysql中sql语句
查看>>
head/tail实现
查看>>
sql语句的各种模糊查询语句
查看>>
vlc 学习网
查看>>
Python20-Day05
查看>>
Real World Haskell 第七章 I/O
查看>>
C#操作OFFICE一(EXCEL)
查看>>
【js操作url参数】获取指定url参数值、取指定url参数并转为json对象
查看>>
ABAP 程序间的调用
查看>>
移动端单屏解决方案
查看>>
web渗透测试基本步骤
查看>>
使用Struts2标签遍历集合
查看>>
angular.isUndefined()
查看>>
第一次软件工程作业(改进版)
查看>>
网络流24题-飞行员配对方案问题
查看>>
Jenkins 2.16.3默认没有Launch agent via Java Web Start,如何配置使用
查看>>
引入css的四种方式
查看>>
iOS开发UI篇—transframe属性(形变)
查看>>
3月7日 ArrayList集合
查看>>