简体   繁体   English

弱事件模式中RemoveHandler的用途是什么?

[英]What is the use of RemoveHandler in Weak Event Pattern?

As per Microsoft doc : 根据Microsoft doc

The weak event pattern can be used whenever a listener needs to register for an event, but the listener does not explicitly know when to unregister . 每当侦听器需要注册事件时都可以使用弱事件模式,但是该侦听器并不明确知道何时注销

A sample code here illustrates that when using WeakEventManager , not explicitly calling RemoveHandler will not cause memory leak. 此处的示例代码说明,使用WeakEventManager ,未显式调用RemoveHandler 不会导致内存泄漏。

So my question is, why is the method RemoveHandler even there in WeakEventManager class? 所以我的问题是,为什么WeakEventManager类中甚至还有方法RemoveHandler Isn't it completely unnecessary? 完全没有必要吗? When it becomes necessary? 什么时候有必要?

Isn't it completely unnecessary? 完全没有必要吗?

No. How are you supposed to unsubsribe (explicitly) if there isn't a RemoveHandler method? 不。如果没有RemoveHandler方法,应该如何(明确地)取消订阅?

When it becomes necessary? 什么时候有必要?

Whenever you want to unsubsribe from the event subscription explicitly. 每当您想明确取消订阅事件订阅时。

If you don't call RemoveHandler , the event handling continues until the garbage collecor collects the listener. 如果不调用RemoveHandler ,则事件处理将继续进行,直到垃圾回收收集了侦听器为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM