简体   繁体   English

取消订阅匿名代表事件

[英]Unsubscribing from anonymous delegate event

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]);

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. 我已经在MSDN上阅读了一些有关取消订阅这些方法的方法,这些方法是通过创建保存这些事件的局部变量,然后通过这种方式取消订阅,但是我似乎无法在自己的项目中使用此方法。 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. 我相信基于对象实例和方法签名的代表是平等的。

A possible duplicate: 可能的重复项:

How to remove a lambda event handler 如何删除Lambda事件处理程序

Basically, keep a reference: 基本上,请保留一个参考:

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. 或者将匿名方法转换为适当的方法。

In short, you can not do that with anonymous delegate. 简而言之,您不能使用anonymous委托来做到这一点。

If you want to have a abbilty to unsubscribe from the event, define a delegate/action and follow a pattern you refer. 如果您想取消订阅该事件,请定义一个delegate/action并遵循您引用的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM