4

我想将一个参数传递给我试图用这段代码实现的事件处理程序:

    private void openInputImagesToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ...
        pb.MouseDoubleClick += new MouseEventHandler((sender,e) => showLargeImage(sender,e,imageIndex));
    }

    private void showLargeImage(object sender, MouseEventArgs e, int imageIndex)
    {
        ...
    }

但是,适配器内的“sender”和“e”变量出现错误:

A local variable named 'sender' cannot be declared in this scope because it would give a different meaning to 'sender', which is already used in a 'parent or current' scope to denote something else.

A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'sender', which is already used in a 'parent or current' scope to denote something else.

我该如何解决这个问题?

4

3 回答 3

4

正如编译器告诉你的那样,你不能命名你的 lambda 方法的参数sender和。e

sender它会在openInputImagesToolStripMenuItem_Click方法的参数和 lambda 方法的参数之间产生歧义。

只需使用其他名称,例如

pb.MouseDoubleClick += new MouseEventHandler((s, e1) => showLargeImage(s, e1,imageIndex));
于 2013-10-22T07:17:09.583 回答
1

应该足以更改sender为其他内容,例如se

 pb.MouseDoubleClick += 
              new MouseEventHandler((se,e) => showLargeImage(se,e,imageIndex));
于 2013-10-22T07:17:30.453 回答
1

您已重新定义sendere在您的 lambda 内部,这是不允许的:

private void openInputImagesToolStripMenuItem_Click(object sender, EventArgs e)
{
    ...
    pb.MouseDoubleClick += new MouseEventHandler((s,x) => showLargeImage(sender,e,imageIndex));
}
于 2013-10-22T07:17:36.707 回答