1

我在我的 WPF 应用程序中使用弱事件管理器将事件源连接到目标,使用 XXXEventManager.AddListener(source, this); 称呼。但是,我注意到我的代码有时会不止一次地连接同一个源和目标。这看起来会导致引发/处理实际上相同的多个事件(即每个连接一个事件)。是否可以通过询问 WeakEventManager(或向其添加功能)来确定目标是否已经连接到事件源,或者我是否必须在目标上实现我自己的列表?

此外,如果事件挂钩在源的生命周期内应该存在,那么调用 RemoveListener 的可接受方法是什么?我听说使用终结器不是一个好习惯,因为它会导致垃圾收集问题。这是准确的吗?

4

1 回答 1

0

Well you could check with GetInvocationList if the Eventmanager is already attached. Check if it is already attached with that manager with a specific target on the other hand i don't know if thats possible. When looking at the PropertychangedEventManager though, it is perfectly fine to add multiple listeners to one manager with one target(distinguished with different property names)

For me the correct way of calling RemoveListener is to use IDisposable. The problem with the finalizer is that your object lives longer, because it will be on the finalizer queue, and might get events at that time(not sure about that). Maybe this will help

于 2011-07-07T15:02:23.400 回答