我实际上会在 ClassB 上创建自己的活动
public event EventHandler MySpecialHook;
EventHandler 是一个标准的委托
public delegate void EventHandler(object sender, EventArgs e);
然后,在您的 Class A 中,在创建 ClassB 的实例之后,当 B 中发生 A 应该知道的事情时,挂钩到事件处理程序以进行通知。很像 OnActivated、OnLostFocus、OnMouseMove 或类似事件(但它们具有不同的委托签名)
public class ClassB {
public event EventHandler MySpecialHook;
public void SomeMethodDoingActionInB()
{
// do whatever you need to.
// THEN, if anyone is listening (via the class A sample below)
// broadcast to anyone listening that this thing was done and
// they can then grab / do whatever with results or any other
// properties from this class as needed.
if( MySpecialHook != null )
MySpecialHook( this, null );
}
}
public class YourClassA
{
ClassB YourObjectToB;
public YourClassA
{
// create your class
YourObjectToB = new ClassB();
// tell Class B to call your "NotificationFromClassB" method
// when such event requires it
YourObjectToB += NotificationFromClassB;
}
public void NotificationFromClassB( object sender, EventArgs e )
{
// Your ClassB did something that your "A" class needs to work on / with.
// the "sender" object parameter IS your ClassB that broadcast the notification.
}
}