定义接口中事件和接口参数
namespace ImplementInterfaceEvents
{
public interface IDrawingObject
{
event EventHandler ShapeChanged;
}
public class MyEventArgs : EventArgs
{
// class members
}
public class Shape : IDrawingObject
{
public event EventHandler ShapeChanged;
void ChangeShape()
{
// Do something here before the event…
OnShapeChanged(new MyEventArgs(/*arguments*/));
// or do something here after the event.
}
protected virtual void OnShapeChanged(MyEventArgs e)
{
ShapeChanged?.Invoke(this, e);
}
}
}
订阅实现:
private Shape shape;
public Form1()
{
InitializeComponent();
shape = new Shape();
shape.ShapeChanged += Shape_ShapeChanged;
}
private void Shape_ShapeChanged(object sender, EventArgs e)
{
MessageBox.Show((e as MyEventArgs).Name);
}
//某个按钮事件里启动
private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
{
shape.ChangeShape();
}
标签:定义,C#,shape,接口,event,Shape,public,MyEventArgs,ShapeChanged From: https://www.cnblogs.com/east115/p/18285033