0

我有这个片段

private void westButton_click(object sender, EventArgs ea)
{
    PlayerCharacter.Go(Direction.West);
}

重复了北,南和东。

如何声明一个函数,让我以编程方式生成类似 ir 的方法?

例如,我希望能够打电话

northButton.Click += GoMethod(Direction.North);

而不是定义方法,然后

northButton.Click += new EventHandler(northButton.Click);
4

3 回答 3

4
northButton.Click += (s, e) => GoMethod(Direction.North);

(或者...)

northButton.Click += (s, e) => PlayerCharacter.Go(Direction.North);
于 2009-08-08T04:08:18.310 回答
0
northButton.Click += (s,e) => GoMethod(Direction.North);
于 2009-08-08T03:57:50.483 回答
0

我似乎找到了解决方案:

private EventHandler GoMethod(Direction dir)
{
    return new EventHandler((object sender, EventArgs ea) => PlayerCharacter.Go(dir));
}

我唯一关心的是绑定时间。如果 PlayerCharacter 是null当我调用 GoMethod 时,会发生什么?

于 2009-08-08T04:07:27.703 回答