silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverlight - Prism Event Aggregator - 事件订阅与退订大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

今天发现一个bug: 场景:某个可以反复打开、关闭的view (每次打开生成一个viewmodel), 在viewmodel的constuctor中使用Prism EventAggregator订阅了一系列事件。 Bug: 反复打开、关闭后,即使某个事件在控件本次打开后,只触发了一次,事件处理方法却被调用多次。 原因: 1. 订阅事件时,使用了Strong References (keepSubs

今天发现一个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 @H_589_27@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 to keep subscriber reference alive,we'll have to remember to unsubscribe from an event once we don't need it anymore to avoid memory leaks.

 

h ttp://www.mokosh.co.uk/post/Prism-2-WPF-and-Silverlight-Events.aspx

 

解决:实现IDisposable接口,在Dispose方法中退订事件

 

此外,Prism (Feb 2009) 在Silverlight上使用week references时不支持事件过滤为Lambda表达式,可用一个方法代替:

 

public


bool

FundOrderFilter(FundOrder fundOrder)


{


return

fundOrder.CustomerId == _customerId;


}


...





FundAddedEvent fundAddedEvent = eventAggregator.GetEvent<FundAddedEvent>();





subscriptionToken = fundAddedEvent.Subscribe(FundAddedEventHandler,Threadoption.UIThread,false

,FundOrderFilter ); http://msdn.microsoft.com/en-us/library/dd458918.aspx

大佬总结

以上是大佬教程为你收集整理的Silverlight - Prism Event Aggregator - 事件订阅与退订全部内容,希望文章能够帮你解决Silverlight - Prism Event Aggregator - 事件订阅与退订所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。