简体   繁体   English

emddudley的WeakEventManager的示例

[英]emddudley's WeakEventManager's sample

I am trying to adapt Michael Dudley's sample code for my own implementation of a weak event manager: 我正在尝试将Michael Dudley的示例代码改编为自己的弱事件管理器实现:

Example implementation of weak events using .NET's WeakEventManager 使用.NET的WeakEventManager实现弱事件的示例实现

I was wondering where would I put my user code for my own handler? 我想知道如何将我的用户代码放在我自己的处理程序中? I see this: 我看到这个:

private EventHandler mHandler = (s, e) =>
{
    CurrentManager.DeliverEvent(s, e);
    return;
};

Would I replace the content of that expression with my own custom code? 我会用自己的自定义代码替换该表达式的内容吗?

Thank you, 谢谢,

Kevin 凯文

For most cases that handler should stay as it is. 在大多数情况下,处理程序应保持原样。 As a comment on that answer states, the handler declaration isn't even necessary. 作为对该答案的评论,甚至不需要处理程序声明。 The following code for StartListening should be sufficient: 以下代码用于StartListening应该足够了:

((PropertyValue)source).Changed += DeliverEvent;

If you need to modify the values of s and/or e before delivering the event you can add that code to the handler you posted. 如果在传递事件之前需要修改s和/或e的值,则可以将该代码添加到发布的处理程序中。 But no matter what, DeliverEvent must be called. 但是无论如何, DeliverEvent必须调用DeliverEvent It loops through and calls ReceiveWeakEvent for all registered listeners and there's no way to get at the list of listeners unless you are storing them in your own ListenerList . 它循环并为所有注册的侦听器调用ReceiveWeakEvent ,除非您将它们存储在自己的ListenerList中,否则无法获得侦听ListenerList So, basically if you modify the event handler, it must still call DeliverEvent or your weak event listeners will never be notified of the event. 因此,基本上,如果您修改事件处理程序,它仍必须调用DeliverEvent否则您的弱事件侦听器将永远不会收到该事件的通知。

If you posted the code you're trying to find a place for, it might be a little easier to help you figure out what you are trying to accomplish and where it should go instead. 如果您发布了要查找位置的代码,可能会更容易一些,以帮助您弄清要完成的任务以及应该去的地方。

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

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