Composite WPF EventAggregator subscriptions being lost

送分小仙女□ 提交于 2019-12-10 15:57:28

问题


In my Composite WPF application I have an event that is published when the user double-clicks on a control. Modules subscribe to the event and perform an action when necessary.

This event seems to stop working at random. Sometimes when I run the application I can trigger the event with no problems, other times I can only trigger it a few times before the modules stop receiving the event.

When I look in the debugger the CAL EventAggregator still has the event, but the event has no subscriptions. How can EventAggregator be losing subscriptions?


回答1:


Turns out it was the garbage collector removing the subscriptions. I'll have to read up on the internals, but when I replaced

this.mEventAggregator.GetEvent<SomeEvent>().Subscribe(SomeFunction);

with

this.mEventAggregator.GetEvent<SomeEvent>().Subscribe(
    SomeFunction, ThreadOption.UIThread, true);

it started working. The UI thread parameters wasn't my problem, but for others it may be important to ensure you're handling the event on the right thread too.



来源:https://stackoverflow.com/questions/1132690/composite-wpf-eventaggregator-subscriptions-being-lost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!