How to subscribe to an event without preventing garbage collection?

六眼飞鱼酱① 提交于 2020-01-24 18:40:49

问题


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;?


回答1:


After writing the and question and doing my last search-before-posting on weak bindings I came across the weak event pattern that does exactly what I want



来源:https://stackoverflow.com/questions/30088798/how-to-subscribe-to-an-event-without-preventing-garbage-collection

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