今天发现一个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