검색결과 리스트
Game Programming/C#에 해당되는 글 1건
- 2010.04.29 C# Event 사용법
글
C# Event 사용법
Game Programming/C#
2010. 4. 29. 15:45
1. event 선언
public event EventHandler EventName;
2. event handler 함수 정의
public void Event_Handler_Fuction(object sender, EventArgs e)
{
// Action
}
3. 이벤트 핸들러 함수 바인딩
Object.EventName += new EventHandler(Event_Handler_Fuction);
4. 이벤트 호출
EventName(this, new EventArgs());
<예제>
A.cs
namespace EventTest
{
class A
{
public static event EventHandler staticAction; // static event 선언
public event EventHandler action; // event 선언
private void Test()
{
staticAction(this, new EventArgs()); // statc 이벤트 호출
action(this, new EventArgs()); // 이벤트 호출
}
}
}
B.cs
namespace EventTest
{
class B
{
// 멤버 변수
private A a = new A();
// 생성자
public B()
{
A.staticAction += new EventHandler(A_Test_Handler); // 이벤트 핸들러 함수 바인딩
a.action += new EventHandler(A_Test_Handler); // 이벤트 핸들러 함수 바인딩
}
public void A_Test_Handler(object sender, EventArgs e) // event handler 함수 정의
{
// do anything
}
}
}