简体   繁体   English

C#:EventHandler委托

[英]C#: EventHandler delegate

public delegate void EventHandler(object sender, EventArgs e);

In the above code, EventHandler is a delegate which accepts object sender, EventArgs e . 在上面的代码中,EventHandler是一个委托,它接受object sender, EventArgs e However, the number of arguments do not match in the below implementation: 但是,参数的数量在以下实现中不匹配:

this.baptismApplication_add_button.Click += new System.EventHandler(this.baptismApplication_add_button_Click);

I can see that the sender is this.baptismApplication_add_button_Click , but which part of the method calls has EventArgs e ? 我可以看到senderthis.baptismApplication_add_button_Click ,但方法调用的哪一部分有EventArgs e Is it implicit? 这是隐含的吗? Or am I going about this all wrong? 或者我这样做是错的?

delegate are special types. delegate是特殊类型。 They represent methods . 它们代表方法 This: 这个:

public delegate void EventHandler(object sender, EventArgs e);

does not mean that to create an EventHandler , you need to pass in two arguments sender and e . 并不意味着创建一个EventHandler ,您需要在两个参数传递sendere It simply means 它只是意味着

EventHandler represents a method with the signature of (object sender, EventArgs e) returning void . EventHandler表示一个签名为(object sender, EventArgs e)返回void

To create a EventHandler , you need to pass in a method that has the required signature. 要创建EventHandler ,您需要传入具有所需签名的方法。 For example, this method: 例如,这种方法:

private void MyMethod(object sender, EventArgs e) {

}

You can create an EventHandler like this: 您可以像这样创建一个EventHandler

new EventHandler(MyMethod);

Note how I did not write () after the method name. 请注意我在方法名称后面没有写() Doing so would call the method. 这样做会调用该方法。 We don't want that. 我们不希望这样。 Here we just want to refer to it. 在这里,我们只想参考它。

Let's look at another example. 让我们看另一个例子。

public delegate void Action();

To create an Action , you need a method that has no arguments and returns void . 要创建Action ,您需要一个没有参数的方法并返回void Like this: 像这样:

private void MyMethod2() {}
// ...
new Action(MyMethod2);

Now that you understood how delegates work, we can talk about how to subscribe to the Click event properly. 现在您已了解委托的工作方式,我们可以讨论如何正确订阅Click事件。

As I've said, EventHandler 's "constructor" takes a method as argument, so you shouldn't pass your button in there. 正如我所说, EventHandler的“构造函数”将一个方法作为参数,所以你不应该在那里传递你的按钮。 You need a method. 你需要一个方法。 Let's write one: 我们来写一个:

private void ButtonPressed(object sender, EventArgs e) {
    Console.WriteLine("Hello World");
}

Now we can create the delegate with this method: 现在我们可以使用此方法创建委托:

yourButton.Click += new EventHandler(this.ButtonPressed);

This can actually be shortened to: 这实际上可以缩短为:

yourButton.Click += ButtonPressed;

Now every time you press the button "Hello World" will be printed. 现在,每次按下“Hello World”按钮都会打印出来。

You're confusing delegate signature and signature of delegate constructor . 您将委托签名和委托构造函数的签名混淆。

Delegate signature is void (object, EventArgs) , but delegate constructor (you call it via new System.EventHandler(...) ) expects a single method with signature above, not a pair of arguments. 委托签名是void (object, EventArgs) ,但委托构造函数(通过new System.EventHandler(...)调用它)期望上面带有签名的单个方法 ,而不是一对参数。

Note, that explicit constructor call could be omitted: 注意,可以省略显式构造函数调用:

this.baptismApplication_add_button.Click += this.baptismApplication_add_button_Click;

Compiler will convert this into EventHandler constructor call. 编译器会将其转换为EventHandler构造函数调用。

+= new System.EventHandler(this.baptismApplication_add_button_Click);

Should be 应该

+= new System.EventHandler(myhandler);

where myhandler is a handler method with proper matching arguments per delegate definition like 其中myhandler是一个处理程序方法,每个委托定义都有适当的匹配参数

private void myhandler(object sender, EventArgs e)
{

}

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

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