How to determine if an event is already subscribed

前端 未结 7 1711
傲寒
傲寒 2020-11-29 02:08

In my .NET application I am subscribing to events from another class. The subscription is conditional. I am subscribing to events when the control is visible and de-subscrib

7条回答
  •  粉色の甜心
    2020-11-29 02:43

    Can't you just remember whether you already subscribed? That approach worked fine for me so far. Even if you have a lot of events or objects, you may still want to just remember that (in a dictionary, for example).

    On the other hand, visibility change was, at least for me, not a good point to subscribe/unsubscribe. I typically rather go with construction / Disposed, which are more clear than each time visibility changes.

提交回复
热议问题