今天发现一个bug:
场景:某个可以反复打开、关闭的view (每次打开生成一个viewmodel), 在viewmodel的constuctor中使用Prism EventAggregator订阅了一系列事件。
Bug: 反复打开、关闭后,即使某个事件在控件本次打开后,只触发了一次,事件处理方法却被调用多次。
原因:
1. 订阅事件时,使用了Strong References (keepSubscriberReferenceAlive 参数值为true)
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 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。