取消订阅匿名代表事件 [英] Unsubscribing from anonymous delegate event

查看:83
本文介绍了取消订阅匿名代表事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种取消订阅的匿名方法时遇到了一些麻烦,这些匿名事件在预制的帮助程序文件中发现,该文件有助于在运行时移动控件。我要取消订阅这些事件的原因是,控件(在这种情况下为按钮)将再次被锁定且无法移动。这是帮助程序类中的方法:

I'm having some trouble with figuring out a way of unsubscribing from some anonymous delegate events that i found in a pre-made helper file that helps allow movement of controls at run time. The reason i want to unsubscribe to these events is so that the control (in this case buttons) will become locked again and not able to be moved. Here is the method in the helper class:

 public static void Init(Control control)
    {
        Init(control, Direction.Any);
    }

    public static void Init(Control control, Direction direction)
    {
        Init(control, control, direction);
    }

 public static void Init(Control control, Control container, Direction direction)
    {
        bool Dragging = false;
        Point DragStart = Point.Empty;

        control.MouseDown += delegate(object sender, MouseEventArgs e)
        {
            Dragging = true;
            DragStart = new Point(e.X, e.Y);
            control.Capture = true;
        };
        control.MouseUp += delegate(object sender, MouseEventArgs e)
        {
            Dragging = false;
            control.Capture = false;
        };
        control.MouseMove += delegate(object sender, MouseEventArgs e)
        {
            if (Dragging)
            {
                if (direction != Direction.Vertical)
                    container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
                if (direction != Direction.Horizontal)
                    container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
            }
        };

    }

这是我通过调用方法来订阅这些事件的方式;

and here's how i subscribe to these events by calling the method;

    ControlMover.Init(this.Controls["btn" + i]);

我已经了解了MSDN上有关通过创建保存这些事件的局部变量来取消订阅这些方法的一些方法然后通过这种方式退订,但是我似乎无法在自己的项目中使用它。我该如何取消订阅这些事件,以便我的控件再次固定在位置上?

I've read about some methods on MSDN about unsubscribing to these by creating a local variable holding these events and then unsubscribing through this way, but i can't seem to get this working in my own project. How do i go about unsubscribing to these events so that my controls become fixed in position again?

推荐答案

不保证匿名代表可以如果不遵循由编译器创建的唯一性,则在取消订阅相同代码的唯一性时,将导致无法取消订阅正确的处理程序。安全地这样做的唯一方法是保留对委托的引用,并使用该引用取消订阅或将其更改为完整方法。

Anonymous delegates are not guaranteed to be unique as created by the compiler, when unsubscribing this lack of uniqueness of the same code will cause it to fail to unsubscribe the correct handler. The only way to do so safely is to keep a reference to the delegate and use that to unsubscribe, or change it to a full method.

基于对象的委托相等我相信实例和方法签名。

Delegates are equal based on object instance and method signature I believe.

可能重复:

如何删除lambda事件处理程序

基本上保留一个引用:

MouseEventHandler handler = (sender, e) =>
        {
            Dragging = true;
            DragStart = new Point(e.X, e.Y);
            control.Capture = true;
        };

control.MouseDown += handler;
control.MouseDown -= handler;

或将匿名方法转换为适当的方法。

Or turn the anonymous method into a proper method.

这篇关于取消订阅匿名代表事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆