如何为静态事件实现WeakEventManager

时间:2012-11-23 15:40:22

标签: c# events static weak-references

我试图通过创建我自己的WeakEventManager来实现msdn之后的WeakEventManager。

我的问题是,现在我有一个静态类StaticPublisher,它正在发布一个静态事件OnSomeStaticEvent

我想从另一个类订阅此事件。这是我现在的代码(简化):

public static class StaticPublisher
{         
//definition of static event
    public static event EventHandler<CancelEventArgs> OnSomeStaticEvent;
    public static void OnSomeStaticEventHandler(CancelEventArgs e)
    {
        EventHandler<CancelEventArgs> handlerCopy = OnVisualDisplayBeforeRemoved;
        if (handlerCopy != null)
            handlerCopy(null, e);
    }
}

我的WeakEventManager看起来像这样(再次跟随msdn):

class OnSomeStaticEventWeakEventManager : WeakEventManager
{
    private OnSomeStaticEventWeakEventManager ()
    {

    }

    /// <summary>
    /// Add a handler for the given source's event.
    /// </summary>
    public static void AddHandler(StaticPublisher, EventHandler<CancelEventArgs> handler)
    {
        if (source== null)
            throw new ArgumentNullException("source");
        if (handler == null)
            throw new ArgumentNullException("handler");

        CurrentManager.ProtectedAddListener(source, handler);
    }
// more code follows down. but is all relative to this part

我的问题是我无法为此事件添加任何侦听器,因为我不允许以StaticPublisher作为参数。这里有解决方法吗?我真的可以在这里使用一些帮助......

0 个答案:

没有答案