English 中文(简体)
利用WeakEvent 静态活动的管理人员
原标题:Using WeakEventManager with a static event

是否有可能利用WakEventManager建立一个静态活动的弱小事听众?

我愿就静态的“四方”组织创建一个弱小的活动听众。 如果我不知道WPF/Silverlight源的寿命,则避免记忆泄露的招标活动。

我由

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;
    }
}

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:

    protected void ProtectedAddListener(object source, IWeakEventListener listener) 
    { 
        Debug.Assert(listener != null, "Listener cannot be null");

        object sourceKey = (source != null) ? source : StaticSource;
        ...
    }
最佳回答

图四:将sender <>/strong>关于“一体行动,履行使命”方法的论点改为:

void OnCompositionTargetRendering(object sender, EventArgs e)
{
    this.DeliverEvent(null, e);
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...