title: Using WeakEventManager with a static event
body: <p>Is it possible to create a weak event listener on a static event using WeakEventManager?</p>
  
  <p>I would like to create a weak event listener on the static CompositionTarget.Rendering event to avoid memory leaks when I don't know the lifetime of the WPF/Silverlight source element.</p>
  
  <p>I implemented the rendering event manager by the <a href="http://msdn.microsoft.com/en-us/library/aa970850.aspx">weak event pattern</a>:</p>
  
  <pre><code>public class RenderingEventManager : WeakEventManager
  {
      public static void AddListener(IWeakEventListener listener)
      {
          CurrentManager.ProtectedAddListener(null, listener);
      }
  
      public static void RemoveListener(IWeakEventListener listener)
      {
          CurrentManager.ProtectedRemoveListener(null, listener);
      }
  
      private static RenderingEventManager CurrentManager
      {
          get
          {
              var managerType = typeof(RenderingEventManager);
              var manager = (RenderingEventManager)GetCurrentManager(managerType);
              if (manager == null)
              {
                  manager = new RenderingEventManager();
                  SetCurrentManager(managerType, manager);
              }
              return manager;
          }
      }
  
      protected sealed override void StartListening(object source)
      {
          CompositionTarget.Rendering += this.OnCompositionTargetRendering;
      }
  
      protected sealed override void StopListening(object source)
      {
          CompositionTarget.Rendering -= this.OnCompositionTargetRendering;
      }
  
      void OnCompositionTargetRendering(object sender, EventArgs e)
      {
          this.DeliverEvent(sender, e);
      }
  }
  </code></pre>
  
  <p>and tested the manager in a simple window:</p>
  
  <pre><code>public partial class Window1 : Window, IWeakEventListener
  {
      public Window1()
      {
          InitializeComponent();
          RenderingEventManager.AddListener(this);
      }
  
      bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
      {
          return true;
      }
  }
  </code></pre>
  
  <p>The ReceiveWeakEvent method is not being called. OnCompositionTargetRendering is called, but it seems like DeliverEvent does not work as I expected.
  Adding listeners to instance events like Button.Click works fine...
  The test project is on .NET 4.0 Client Profile.</p>
  
  <p><a href="http://connect.microsoft.com/VisualStudio/feedback/details/393127/the-weakeventmanager-doesnt-work-with-static-events">A post on Microsoft Connect from 2009</a> states that static events are not supported,
  but I checked the WeakEventManager source code, and it looks ok:</p>
  
  <pre><code>    protected void ProtectedAddListener(object source, IWeakEventListener listener) 
      { 
          Debug.Assert(listener != null, "Listener cannot be null");
  
          object sourceKey = (source != null) ? source : StaticSource;
          ...
      }
  </code></pre>