mercredi 6 mai 2015

How to subscribe to an event without preventing garbage collection?

Let's say AppConfiguration.Instance is a singleton.

Now let's say my UI dynamically adds a button that should change it's text if the configuration is change, so my app could do:

AppConfiguration.Instance.Changed += Changed_Handler;

On the button's code, but I don't wanna do that because that will prevent garbage collecion of the button after the user navigates to another screen and the button gets removed from the form

My question is: Is there a design pattern for listening to an event without preventing garbage collection asides from manually unsubscribing to the event with AppConfiguration.Instance.Changed -= Changed_Handler;?

Note: I have seen the concept of a weak binding before (data bindings that don't prevent garbage collection) so I expect a "yes it's possible answer" with some similar functioning to weak bindings.

Aucun commentaire:

Enregistrer un commentaire