Silverlight - Prism Event Aggregator - 事件订阅与退订

博客讨论了在Silverlight中使用Prism Event Aggregator时遇到的一个bug,即由于未退订事件导致处理方法被多次调用。问题源于在订阅事件时使用了强引用,并且在关闭视图时未取消订阅。作者指出,CompositePresentationEvent默认维护对订阅者处理方法的弱引用以防止内存泄漏,但频繁大量事件可能导致性能下降。解决方法是实现IDisposable接口并在Dispose中退订事件。此外,Prism在Silverlight上的弱引用不支持Lambda事件过滤,需要使用替代方法。
摘要由CSDN通过智能技术生成

今天发现一个bug:

场景:某个可以反复打开、关闭的view (每次打开生成一个viewmodel), 在viewmodel的constuctor中使用Prism EventAggregator订阅了一系列事件。

Bug: 反复打开、关闭后,即使某个事件在控件本次打开后,只触发了一次,事件处理方法却被调用多次。

原因:

1. 订阅事件时,使用了Strong References (keepSubscriberReferenceAlive 参数值为true)

2. 关闭View时,没有将之前的事件订阅退订

 

 

by default, CompositePresentationEvent maintains weak reference to subscriber's handling method to avoid memory leaks . This means that reference that it holds will not prevent garbage collector from collecting the subscriber.
For most applications this is what we really want, but if you publish large amouts of events very frequently, it may lead to poor performance. keepSubscriberReferenceAlive is the parameter of Subscribe() mathod that controls which (strong/weak) reference is being held.
If we decide

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值