更新时间:2023-02-25 12:03:31
首先通过为事件参数创建类开始。
First start off by creating a class for your event arguments.
// this can house any kind of information you want to send back with the trigger
public class MyNewEventArgs : EventArgs { ... }
接下来,创建该控件的类的事件。这是在使用委托做的,而事件本身。
Next, create the event on the control's class. this is done using a delegate, and the event itself.
// event delegate handler
public delegate void MyNewEventHandler(object s, MyNewEventArgs e);
// your control class
public class MyControl : Control
{
// expose an event to attach to.
public event MyNewEventHandler MyNewEvent;
接下来,你需要从你的code触发事件。我们通过抓住事件,检查用户,然后触发做到这一点。
Next you need to fire the event from your code. We do this by grabbing the event, checking for subscribers, then triggering.
// grab a copy of the subscriber list (to keep it thread safe)
var myEvent = this.MyNewEvent;
// check there are subscribers, and trigger if necessary
if (myEvent != null)
myEvent(this, new MyNewEventArgs());
更多信息可以在MSDN上找到在如何创建活动的。