是否有可能利用WakEventManager建立一个静态活动的弱小事听众?
我愿就静态的“四方”组织创建一个弱小的活动听众。 如果我不知道WPF/Silverlight源的寿命,则避免记忆泄露的招标活动。
我由
并用简单窗口对管理人员进行测试: 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. A post on Microsoft Connect from 2009 states that static events are not supported,
but I checked the WeakEventManager source code, and it looks ok: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);
}
}
public partial class Window1 : Window, IWeakEventListener
{
public Window1()
{
InitializeComponent();
RenderingEventManager.AddListener(this);
}
bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
return true;
}
}
protected void ProtectedAddListener(object source, IWeakEventListener listener)
{
Debug.Assert(listener != null, "Listener cannot be null");
object sourceKey = (source != null) ? source : StaticSource;
...
}