문제

이 스 니펫이 있습니다

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

북쪽, 남쪽 및 동쪽으로 반복됩니다.

프로그래밍 방식으로 IR과 같은 방법을 생성 할 수있는 함수를 어떻게 선언 할 수 있습니까?

예를 들어, 전화 할 수 있기를 원합니다

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

방법을 정의하는 대신

northButton.Click += new EventHandler(northButton.Click);
도움이 되었습니까?

해결책

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

(또는...)

northButton.Click += (s, e) => PlayerCharacter.Go(Direction.North);

다른 팁

해결책을 찾은 것 같습니다.

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

나의 유일한 관심사는 바인딩 시간에 관한 것입니다. PlayerCharacter라면 null 내가 Gomethod에게 전화 할 때 어떻게 될까요?

northButton.Click += (s,e) => GoMethod(Direction.North);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top