1. Trang chủ
  2. » Công Nghệ Thông Tin

Delegate & Event - Nguyen Ha Giang potx

52 280 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Delegate & Event - Nguyen Ha Giang
Tác giả Nguyen Ha Giang
Trường học Hanoi University of Science and Technology
Chuyên ngành Computer Science
Thể loại Lecture Notes
Năm xuất bản 2012
Thành phố Hanoi
Định dạng
Số trang 52
Dung lượng 507,48 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Event & Delegate• Khai báo delegate xử lý sự kiện • Khai báo event • Các lớp muốn xử lý khi sự kiện OnEventName phát sinh thì phải thực thi event handler public delegate void HandlerName

Trang 1

Delegate & Event

Nguyen Ha Giang

Trang 2

§ Event & delegate

§ Publishing & subscribing

§ Demo using event

Trang 3

Definition of delegate

• Class package some signature methods

• Using in event-handling model of C#

• Like C/C++ method pointers, but more

specific

– Type safe

– Object- oriented mechanism

• Delegates are classes

– Can create instances of delegates

– Delegates can refer one or more methods

Trang 4

Definition of delegate

• Delegates define a method signature

– Return type

– Sequence of parameter types

• Any method has same signature can add to

instance of delegate

• Delegate instances have the list of method

references

– Using “+=” for adding method to instance

– Using “-=” for removing method out of instance

Trang 5

Working with delegates

• Define a delegate in namespace or class

public delegate void MyDelegate1 (int x, int y);

Delegate for methods: void ABC( int , int )

public delegate string MyDelegate2 (float f);

Delegate for methods: string XYZ( float )

Trang 6

Working with delegates

• Instance of delegate

class Test {

public void Method1(int a, int b) {

// body of method }

… }

Test t = new Test();

MyDelegate1 d1 = new MyDelegate1 ( t.Method1);

public delegate void MyDelegate1 (int x, int y);

Trang 7

Working with delegate

• Instance of delegate

class Test {

… public static string Method2(float f) {

// body of method }

}

MyDelegate2 d2 = new MyDelegate2 ( Test.Method2);

public delegate string MyDelegate2 (float f);

Trang 8

Working with delegate

Trang 9

Invoking multiple delegate

• Using only for delegates with no return value

class Test

{ public delegate voidMyDelegate3(int n1, int n2);

static void Print(int x, int y) {

Console.WriteLine("x={0}, y={1}", x, y);

} static void Sum(int a, int b) {

Console.WriteLine("Tong={0}", a + b);

} // (cont)…

}

No return

Trang 10

Invoking multiple delegate

x=5, y=10 Tong=15 Tong=110

Trang 12

How to build the

Sort method for array of objects of any data type?

Trang 14

• Objects have to define their order (Compare)

• Using delegate to pass “ compare method ” into Sort method.

void Sort(object[] list, CompareObj cmp)

This delegate refers to Compare method of

class which need to sort.

Trang 15

• Define delegate CompareObj for Sort method

public delegate int CompareObj( object o1, object o2)

Trang 16

Definition of Sort method Delegate will refer to compare method of class

Invoking delegate (refer Compare method of

class)

Trang 17

• Class support Sort method required

– Provide a Compare method (or more)

– Method signature is same with the delegate

class Person {

private string name;

private int weight;

private int yearOfBirth;

public static int CompareName (object p1, object p2)

{

return string.Compare(((Person)p1).name, ((Person)p2).name); }

}

Trang 18

//…

Person[ ] persons = new Person[4];

persons[0] = new Person(“ Quy Mui ", 20, 2004);

persons[1] = new Person(“ Ha Giang ", 62, 1978);

persons[2] = new Person(“ Ngoc Thao ", 47, 1979);

persons[3] = new Person(“ Ha Nam ", 4, 2009);

// create instance of delegate refer to Person.CompareName

CompareObj cmp = new CompareObj (Person CompareName );

HaGLib.Sort( persons , cmp );

Class contains static Sort method

Trang 21

• Publisher : lớp phát sinh sự kiện

• Subscriber : lớp nhận hay xử lý khi sự kiện xảy ra

Trang 22

• Trong môi trường giao diện GUIs (Graphical User Interfaces: GUIs):

– Button đưa ra sự kiện “Click”, cho phép lớp khác

có thể đáp ứng (xử lý) khi sự kiện này xảy ra

• VD: Button “Add” trong Form, khi sự kiện

click xảy ra thì Form thực hiện lấy dữ liệu từ các TextBox đưa vào ListBox…

Trang 23

• Một lớp publish tập các event cho phép các lớp khác subscribe

– Button là lớp publish đưa ra event: click

– Form là lớp subscribe có phần xử lý riêng khi

“click” của Button kích hoạt

Trang 24

Event & Delegate

• Sự kiện trong C# được thực thi nhờ uỷ thác

– Lớp publishing định nghĩa ủy thác

– Những lớp subscribing phải thực thi

– Khi sự kiện xuất hiện thì phương thức của lớp

subscribing được gọi thông qua uỷ thác

• Phương thức để xử lý sự kiện gọi là trình xử lý

sự kiện (event handler)

Trang 25

Event & Delegate

• Trình xử lý sự kiện trong NET Framework

được mô tả như sau:

Trang 26

Event & Delegate

• Khai báo delegate xử lý sự kiện

• Khai báo event

• Các lớp muốn xử lý khi sự kiện OnEventName phát sinh thì phải thực thi event handler

public delegate void HandlerName(object obj, EventArgs arg);

public event HandlerName OnEventName;

Trang 27

Minh họa

• Xây dựng 1 lớp thực hiện yêu cầu: “cứ mỗi

giây sẽ phát sinh 1 sự kiện”

• Cho phép 2 lớp khác đăng ký xử lý sự kiện

này, mỗi lớp có cách xử lý riêng:

– Lớp A: hiển thị thời gian theo “mô phỏng đồng hồ

analog”

– Lớp B: hiển thị thời gian theo “mô phỏng đồng hồ

digital”

Trang 28

Minh họa

• Tạo một lớp Clock:

– Khai báo một event: OnSecondChange

– Một phương thức Run: cứ 1s thì phát sinh sự kiện OnSecondChange

• Tạo 2 lớp: AnalogClock và DigitalClock nhận

xử lý sự kiện OnSecondChange của lớp Clock

Trang 29

Minh họa

• Khai báo delegate xử lý event

Tên delegate xử lý sự kiện

Đối tượng phát sinh event

Tham số kiểu EventArgs delegate void SecondChangeHandler (object clock , EventArgs info);

Trang 30

Minh họa

• Khai báo event có hàm xử lý mô tả trên

event SecondChangeHandler OnSecondChange ;

Kiểu delegate

Tên của event

Từ khóa event: thể hiện cơ chế publishing & subscribing

Trang 31

Minh họa

• Kích hoạt sự kiện

if ( OnSecondChange != null)

OnSecondChange(this, new EventArgs());

Gọi hàm xử lý sự kiện đã đăng ký

Kiểm tra xem có hàm xử lý được đăng ký hay không?

Trang 32

Minh họa

public class Clock {

public delegate void

SecondChangeHandler (object clock, EventArgs info);

public event SecondChangeHandler OnSecondChange ;

public void Run() {

while (true) { Thread.Sleep(1000);

if ( OnSecondChange != null)

OnSecondChange (this, new EventArgs());

} }

}

Trang 33

Minh họa

• Lớp DigitalClock

– Định nghĩa trình xử lý sự kiện của Clock

• Đúng mô tả delegate hàm xử lý của lớp Clock

• Thực hiện một số thao tác riêng của DigitalClock

– Đăng ký xử lý với trình xử lý sự kiện trên khi có

sự kiện OnSecondChange của Clock

• Chức năng đăng ký với lớp Clock là có xử lý khi sự kiện OnSencondChange của Clock phát sinh

• Ủy thác cho lớp Clock sẽ gọi trình xử lý định nghĩa bên trên của DigitalClock

Trang 37

Minh họa

• Lớp AnalogClock

– Định nghĩa trình xử lý sự kiện của Clock

• Đúng mô tả delegate hàm xử lý của lớp Clock

• Thực hiện một số thao tác riêng của AnalogClock

– Đăng ký xử lý với trình xử lý sự kiện trên khi có

sự kiện OnSecondChange của Clock

• Chức năng đăng ký với lớp Clock là có xử lý khi sự kiện OnSencondChange của Clock phát sinh

• Ủy thác cho lớp Clock sẽ gọi trình xử lý định nghĩa bên trên của AnalogClock

Trang 39

Minh họa

• Minh họa cơ chế event

public class Tester {

public static void Main() {

Clock myClock = new Clock();

AnalogClock c1 = new AnalogClock();

DigitalClock c2 = new DigitalClock();

Trang 40

Minh họa

public static void Main( ) {

Clock myClock = new Clock();

AnalogClock c1 = new AnalogClock();

DigitalClock c2 = new DigitalClock();

myClock OnSecondChange += new

Trang 42

Minh họa EventArgs

– Khi phát sinh sự kiện, truyền thời gian hiện hành

⇒ lớp subscribing sẽ sử dụng tham số này

– Tạo một lớp TimeEventArgs kế thừa từ EventArgs: chứa thời gian sẽ truyền đi

Trang 43

Minh họa EventArgs

• Tạo lớp chứa tham số truyền cho trình xử lý sự kiện

– Lớp dẫn xuất từ EventArgs

– Chứa các thông tin về: giờ, phút, giây

• Bắt buộc phải dẫn xuất từ EventArgs

– Do mô tả của trình xử lý sự kiện là tham số thứ 2 phải là lớp dẫn xuất từ EventArgs!

Trang 44

Minh họa EventArgs

public class TimeEventArgs : EventArgs

{

public readonly int Second;

public readonly int Minute;

public readonly int Hour;

public TimeEventArgs(int s, int m, int h) {

Second = s;

Minute = m;

Hour = h;

} }

Trang 45

Minh họa EventArgs

• Trong lớp Clock khai báo trình xử lý sự kiện như sau

public delegate void SecondChangeHandler(object obj, TimeEventArgs arg);

Sử dụng tham số thứ hai có kiểu TimeEventArgs

Trang 46

Minh họa EventArgs

• Khi kích hoạt sự kiện thì truyền tham số {giờ, phút, giây}

public void Run( ) {

while (true) { Thread.Sleep(1000);

if (OnSecondChange != null) { DateTime date = DateTime.Now;

TimeEventArgs timeArg = new

TimeEventArgs(date.Second, date.Minute, date.Hour); OnSecondChange(this, timeArg);

} } }

Trang 47

Minh họa EventArgs

• Các lớp DigitalClock và AnalogClock: sử

dụng tham số truyền vào

public class AnalogClock {

public void Subcribe(Clock theClock)

Trang 48

Minh họa EventArgs

• Các phần khác còn lại tương tự như minh họa 1

Trang 49

• Bài tập

Vi t m∀t ch%&ng trình ∋&n gi(n minh h∗a qu(n lý tài kho(n ATM: khi rút ti+n ho,c chuy.n ti+n thì h/ th0ng s1 g3i t4 ∋∀ng tin nh5n ∋ n handphone c6a ch6 tài kho(n.

H%8ng d:n:

- Khi rút ti+n ho,c chuy.n ti+n xong: phát sinh s4 ki/n “∋ã rút ti+n” ho,c “∋ã chuy.n ti+n”

Trang 50

Tóm tắt

• Delegate

– Cho phép tạo thể hiện

– Tham chiếu đến một phương thức

– Multi cast delegate tham chiếu đến nhiều phươngthức

– Multi cast là delegate có giá trị trả về phải là void

– Các phương thức được ủy quyền phải thoả

signature method của delegate

– Khi delegate được gọi nó sẽ thực thi tất cả các

phương thức được ủy quyền

Trang 51

Tóm tắt

• Event

– Event được thực thi thông qua delegate

– Thể hiện sự truyền thông qua lại

– Lớp phát sinh sự kiện: publishing

– Những lớp xử lý sự kiện: subscribing

– Thành phần quan trọng trong GUIs

Trang 52

Delegate & Event

Ngày đăng: 28/03/2014, 16:20

TỪ KHÓA LIÊN QUAN

w