在一个类中连接事件处理程序并在另一个类中断开它。 [英] Connecting an event handler in one class and disconnecting it in a different class.

查看:53
本文介绍了在一个类中连接事件处理程序并在另一个类中断开它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,A类实例化了B和C类。


我希望B类将事件处理程序连接到A类中的方法,并且

用于C类断开该事件处理程序。


我认为我做了太多思考,现在我更加困惑

如何在我开始时完成此任务。


任何帮助将不胜感激。

解决方案

< blockquote>


感谢发布。以下代码供您参考:


使用系统;


public delegate void ADelegate();


公共类A

{

public static void Main(string [] args)

{

B b = new B();

ADelegate d = new ADelegate(AMethod);

b.AEvent + = d;

C c = new C();

c.RemoveDelegateFromB(d,b);

}


public static void AMethod( )

{

Console.WriteLine(方法);

}

}


公共舱B

{

公共活动ADelegate AEvent;

public void RaiseAEvent()

{

if(AEvent!= null)

{

AEvent();

}

}

}


公共舱C

{

public void RemoveDelegateFromB(ADelegate d,B b)

{

b.AEvent - = d;

}

}


我希望这个PS。如果还有其他任何我可以帮忙的话,请感觉

免费发布在这里。


问候,


Felix Wang

Microsoft在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有保证并且没有授予任何权利。


假设ClassB和ClassC引用了ClassA,那么应该

为否这样做有问题。


class ClassA

{

ClassB cl_B = null;

ClassC cl_C = null;

面板pn_Panel = null

public ClassA()

{

pn_Panel = new Panel( );

cl_B =新的ClassB(this);

cl_C =新的ClassC(this);

}

public void Dispose()

{

if(cl_C!= null)

cl_C = null;

}

protected virtual void OnMouseDown(object

sender,System.Windows.Forms.MouseEventArgs e)

{

if(i_DragDrop == 0)

{

OnDragDrop(eX,eY); //做必须做的事情并检查结果,设置

i_DragDrop如果找到了什么

} // if(i_DragDrop == 0)

} // protected override void OnMouseDown(object

sender,System.Windows.Forms.MouseEventArgs e)

} // A类


class ClassB

{

public ClassB(ClassA cl_A)

{

cl_A.pn_Panel。 MouseDown + = new

System.Windows.Forms.MouseEventHandler(cl_A.OnMous eDown);

}

} // ClassB
class ClassC

{

ClassA cl_A = null;

public ClassC(ClassA clA)

{

cl_A = clA;

}

public void Dispose()

{

if(cl_A!= null)

cl_A.pn_Panel.MouseDown - = new

System.Windows.Forms.MouseEventHandler(cl_A.OnMous eDown);

}

} // ClassC


这应该可行。

这适用于我的项目,其中a ClassA将ClassB称为总结ClassB

将清理它在关闭时在ClassA中所做的事情。

注意:由于ClassA只在这里构建ClassB时需要,所以它是

没有像ClassC那样保存到Class Field。


Mark Johnson,柏林德国
mj ***** @ mj10777.de


" SunshineGirl" < BL ** @ blah.com> schrieb im Newsbeitrag

新闻:10 ************* @ corp.supernews.com ...

在我的代码中,A类实例化B和C类。

我希望B类将事件处理程序连接到A类中的方法,
和C类以断开该事件处理程序。

我想我做了太多思考,现在我更加困惑的是如何在我开始时如何实现这一点。

任何帮助都将是赞。



不完全。


A类必须保存事件的代码。

B类必须连接一个事件处理程序(+ =),而不是引发事件。

C类必须断开事件处理程序( - =)。


这是一个已经在运行的应用程序,我正在尝试添加

功能。当用户启动应用程序时,B类使用Windows工具接收

通知。当用户终止

应用程序时,C类使用Windows

工具来接收通知。 A类实例化B类和C类。类B和C

彼此不了解。


应用程序当前监视用户何时拥有推出或终止

申请。我正在尝试添加以下功能。


每当B类收到Internet Explorer启动的通知时,

它需要连接BeforeNavigate2事件处理程序到A级方法

(所以B级必须做+ =东西:ie.BeforeNavigate2 + = new

SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(this.ie_BeforeNavigat
e2)。每当C类收到Internet Explorer已被终止的通知时,它需要断开BeforeNavigate2事件处理程序(所以

C类必须做-= thing:ie.BeforeNavigate2 - = new

SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(this.ie_BeforeNavigat

e2)。


Felix Wang< v - ***** @ online.microsoft.com>在留言中写道

新闻:sT ************* @ cpmsftngxa07 .phx.gbl ...

您好,

感谢您发帖。以下代码供您参考:

使用系统;

公共委托void ADelegate();

公共类A
{public static void Main( string [] args)
{b / B b = new B();
ADelegate d = new ADelegate(AMethod);
b.AEvent + = d;
C c = new C();
c.RemoveDelegateFromB(d,b);
}

public static void AMethod()
{
控制台.WriteLine(A方法);
}

公共课B
公共活动ADelegate AEvent;
公开void RaiseAEvent()
{
if(AEvent!= null)
{
AEvent();
}
}
}

公共课C
{public void RemoveDelegateFromB(ADelegate d,B b)
{
b.AEvent - = d;
}
}

我希望这会有所帮助。如果还有其他任何我可以提供的帮助,请随时在此处发帖。

问候,

王菲利克斯
微软在线合作伙伴支持<获得安全! - www.microsoft.com/security
此帖子提供就像没有保证,也没有赋予任何权利。



In my code, class A instanciates classes B and C.

I would like class B to connect an event handler to a method in class A, and
for class C to disconnect that event handler.

I think I''ve done too much thinking, and now I''m even more confused as to
how to accomplish this as when I started.

Any help will be appreciated.

解决方案

Hi,

Thanks for posting. The following code is for your reference:

using System;

public delegate void ADelegate();

public class A
{
public static void Main(string[] args)
{
B b = new B();
ADelegate d = new ADelegate(AMethod);
b.AEvent += d;
C c = new C();
c.RemoveDelegateFromB(d, b);
}

public static void AMethod()
{
Console.WriteLine("A method");
}
}

public class B
{
public event ADelegate AEvent;
public void RaiseAEvent()
{
if (AEvent != null)
{
AEvent();
}
}
}

public class C
{
public void RemoveDelegateFromB(ADelegate d, B b)
{
b.AEvent -= d;
}
}

I hope this helps. If there is anything else I can help with, please feel
free to post here.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Assuming that ClassB and ClassC have a reference to ClassA then there should
be no Problem in doing this.

class ClassA
{
ClassB cl_B=null;
ClassC cl_C=null;
Panel pn_Panel=null
public ClassA()
{
pn_Panel = new Panel();
cl_B = new ClassB(this);
cl_C = new ClassC(this);
}
public void Dispose()
{
if (cl_C != null)
cl_C=null;
}
protected virtual void OnMouseDown(object
sender,System.Windows.Forms.MouseEventArgs e)
{
if (i_DragDrop == 0)
{
OnDragDrop(e.X,e.Y); // Do what must be done and check the results, set
i_DragDrop if something found
} // if (i_DragDrop == 0)
} // protected override void OnMouseDown(object
sender,System.Windows.Forms.MouseEventArgs e)
} // Class A

class ClassB
{
public ClassB(ClassA cl_A)
{
cl_A.pn_Panel.MouseDown += new
System.Windows.Forms.MouseEventHandler(cl_A.OnMous eDown);
}
} // ClassB
class ClassC
{
ClassA cl_A=null;
public ClassC(ClassA clA)
{
cl_A = clA;
}
public void Dispose()
{
if (cl_A != null)
cl_A.pn_Panel.MouseDown -= new
System.Windows.Forms.MouseEventHandler(cl_A.OnMous eDown);
}
} // ClassC

This should work.
This works in my Projects where a ClassA calls ClassB assuming that ClassB
will clean up what it has done in ClassA when closing.
Note : since ClassA is only needed in the Construction of ClassB here, it is
not saved to a Class Field as in ClassC.

Mark Johnson, Berlin Germany
mj*****@mj10777.de

"SunshineGirl" <bl**@blah.com> schrieb im Newsbeitrag
news:10*************@corp.supernews.com...

In my code, class A instanciates classes B and C.

I would like class B to connect an event handler to a method in class A, and for class C to disconnect that event handler.

I think I''ve done too much thinking, and now I''m even more confused as to
how to accomplish this as when I started.

Any help will be appreciated.



Not quite.

Class A must hold the code for the event.
Class B must connect an event handler (+=), not raise the event.
Class C must disconnect the event handler (-=).

This is an already running application to which I''m trying to add
functionality. Class B uses Windows instrumentation to receive a
notification when the user launches an application. Class C uses Windows
instrumentation to receive a notification when the user terminates an
application. Class A instanciates both classes B and C. Classes B and C
don''t know about each other.

The application currently monitors when the user has launched or terminated
applications. I''m trying to add the following functionality.

Whenever class B receives notification that Internet Explorer has launched,
it needs to connect the BeforeNavigate2 event handler to a method in class A
(so class B must do the += thing: ie.BeforeNavigate2 += new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(this.ie_BeforeNavigat
e2). Whenever class C receives notification that Internet Explorer has been
terminated, it needs to disconnect the BeforeNavigate2 event handler (so
class C must do the -= thing: ie.BeforeNavigate2 -= new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(this.ie_BeforeNavigat
e2).

"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:sT*************@cpmsftngxa07.phx.gbl...

Hi,

Thanks for posting. The following code is for your reference:

using System;

public delegate void ADelegate();

public class A
{
public static void Main(string[] args)
{
B b = new B();
ADelegate d = new ADelegate(AMethod);
b.AEvent += d;
C c = new C();
c.RemoveDelegateFromB(d, b);
}

public static void AMethod()
{
Console.WriteLine("A method");
}
}

public class B
{
public event ADelegate AEvent;
public void RaiseAEvent()
{
if (AEvent != null)
{
AEvent();
}
}
}

public class C
{
public void RemoveDelegateFromB(ADelegate d, B b)
{
b.AEvent -= d;
}
}

I hope this helps. If there is anything else I can help with, please feel
free to post here.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.



这篇关于在一个类中连接事件处理程序并在另一个类中断开它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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