弱的事件使用.NET的WeakEventManager的示例实施 [英] Example implementation of weak events using .NET's WeakEventManager
问题描述
有一个示例实现使用弱事件.NET的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.weakeventmanager.aspx">WeakEventManager?
Is there an example implementation of weak events using .NET's WeakEventManager?
我想通过文档中继给继承来实现它,但它是模糊的。例如,我无法弄清楚如何在我的自定义的经理叫 ProtectedAddListener
从我的静态的addListener
功能。
I'm trying to implement it by following the "Notes to Inheritors" in the documentation, but it is vague. For example, I can't figure out how to call ProtectedAddListener
from my static AddListener
function in my custom manager.
推荐答案
我在之后的<一节的继承者注意事项的指导方针搞明白了我自己href="http://msdn.microsoft.com/en-us/library/system.windows.weakeventmanager.aspx">WeakEventManager文档。这里的 WeakEventManager的
的基本实现。类采购事件被命名为为PropertyValue
,而该事件被命名为更改
。
I figured it out on my own following the guidelines in the "Notes for Inheritors" section of the WeakEventManager documentation. Here's a basic implementation of WeakEventManager
. The class sourcing the event is named PropertyValue
and the event is named Changed
.
public class PropertyValueChangedEventManager : WeakEventManager
{
public static PropertyValueChangedEventManager CurrentManager
{
get
{
var manager_type = typeof(PropertyValueChangedEventManager);
var manager = WeakEventManager.GetCurrentManager(manager_type) as PropertyValueChangedEventManager;
if (manager == null)
{
manager = new PropertyValueChangedEventManager();
WeakEventManager.SetCurrentManager(manager_type, manager);
}
return manager;
}
}
public static void AddListener(PropertyValue source, IWeakEventListener listener)
{
CurrentManager.ProtectedAddListener(source, listener);
}
public static void RemoveListener(PropertyValue source, IWeakEventListener listener)
{
CurrentManager.ProtectedRemoveListener(source, listener);
}
protected override void StartListening(object source)
{
((PropertyValue)source).Changed += DeliverEvent;
}
protected override void StopListening(object source)
{
((PropertyValue)source).Changed -= DeliverEvent;
}
}
这篇关于弱的事件使用.NET的WeakEventManager的示例实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!