Lập trình Windows - Lập Trình C #- Lập Trình C Shap - Chương 4 ppsx

56 559 0
Lập trình Windows - Lập Trình C #- Lập Trình C Shap - Chương 4 ppsx

Đ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

Chapter 4 Delegate & Event               !  !  !"  #$%&"&&  '$%! (    )*+,- . /%0 1,%2%3 4    1$1%25167 121682+%9!: %25163  - . 1$1; #<<3 =    216*&51*& 1*>?@A3& B2C/,- . !DE9F! 255EF GH IJK &LE&)7 &FM N    **&7?+*&&%? *+!O*&3 IJKLM  P2@&%1)%2%&Q*R ?8*+!DE9F!255 EF1S3 ITJKU%9?8!V ?- &9E&)7&F W)XMY  P/&% Z[LE9FM \    2]*P2@&%7?+'> &%1+?)3 ITJK #3^_E`aKWbY`7 E)(3=7N\FFM         cE7&F W <&M Y  &E7F W dM Y  G[ K E7F G[ K E7F G9G*9[ 9E7FM         G9G*9[ 9E7FM GG9& 9[*[ !3 )b !"# !"#  e#f)3b7& [K  e#f3b7& 9g &9G9BK &!hiEFM &!UG9EFWY hi)Z[hiEUG9FM hiZUG9M [...]... method clickButton_Click đươ c gọi thì phải gán method này cho delegate bằng lê nh sau: ê this.button1.Click += new System.EventHandler(this.button1_Click); 31 Event delegate Có thể x c định nhiều method kh c nhau để đáp ứng cho cùng 1 sự kiêên bằng c ch thêm c c delegate kh c nhau  Event delegates là multicast (môêt tâêp delegate có cùng chung 1 signature)  cho phép nhiều method đươ c gọi... action could be caused by user interaction, such as a mouse click, or it could be triggered by some other program logic The object that raises (triggers) the event is called the event sender The object that captures the event and responds to it is called the event receiver 27 Event trong GUI  Trong môi trường GUIs (Graphical User Interfaces):  C c control phát ra sự kiêên (event sender)  Event-handling... theo quy t c nào 19 Solution   Cho phép đối tượng tự quy định thứ tự của chúng Sử dụng delegate để truyền phương thư c so sánh này vào hàm Sort void Sort(object[] list, CompareObj cmp) Delegate này sẽ tham chiếu tới hàm Compare của lớp MyClass Chính lớp MyClass sẽ quy định thứ tự của c c đối tượng 20 Solution  Mô tả delegate CompareObj cho hàm Sort: Tên của delegate public delegate... delegate bool CompareObj(object o1,object o2) Trả về true: nếu o1 “trươ c o2 false: ngươ c lại 2 đối tượng c ̀n so sánh 21 Solution Định nghĩa hàm Sort tổng quát cho c c lớp Delegate sẽ trỏ tới hàm Compare riêng của lớp tương ứng public static void Sort(object[] objs, CompareObj cmp) { for(int i=0; i < objs.Length-1; i++) for(int j=objs.Length-1; j>i; j ) if ( cmp( objs[j], objs[j-1] ) ) {... argument), chứa thông tin về sự kiêên xảy ra EventArgs là lớp c sở của tất c class biểu diễn thông tin về sự kiê n ê Phải th c hiện trình xử lý sự kiện theo đúng mẫu trên!  29 Delegate    Event handler đươ c nối với event của control thông qua 1 object đă c biêêt đươ c gọi là delegate Đối tương delegate giữ tham chiếu đến method có cùng signature như khai báo của delegate C c control... in C# Step 3: Define a class to receive the events This class is usually the main application class containing the Main() - The signature of the event handler must be identical to that of the public delegate created in step 1 public void OnMyEvent(object sender, EventArgs e) { // handle the event } - Instantiate the event generator class created in step 2: EventClass eventObj = new EventClass(); - Add... class Test { delegate int MyDelegate(int p, int q); static void Main() { MyDelegate arithMethod = null; } } 13 Multicast Delegates Môêt delegate có thể chứa nhiều hơn 1 method có cùng signature  Loại delegate này đươ c gọi là multicast delegate  Multicast delegate la loại con của System.MulticastDelegate, mà chính nó lại là lớp con của System.Delegate  "The return type of a multicast... be void"  14 Thư c thi Multicast Delegate  Đươ c khai báo giống như khai báo delegate thông thường nhưng kiểu trả về phải là void delegate void MyMulticastDelegate(int p, int q); 15 Multicast delegate's invocation đươ c thêm vào danh sách gọi của  C c method kh c   multicast delegate bằng toán tử gán '+=‘ Ví dụ MyMulticastDelegate arithMethod = null; arithMethod = new MyMulticastDelegate(Add);... objs[j], objs[j-1] ) ) { Swap( objs[j], objs[j-1] ); } Yêu c ̀u lớp tự so sánh } 22 Solution  C c lớp hỗ trợ Sort thì phải  Cung c ́p hàm Compare riêng  Signature phải thoả delegate CompareObj class Person { Cùng private string name; signature private int weight; private int yearOfBirth; public static bool CompareName(object p1, object p2) { if (string.Compare(((Person)p1).name, ((Person)p2).name) . '(   2C l)?l/)&d9 VS93*>+216 2C l& ;- )  ITJK 2C l92m .  EF  W 9E7FM !EF W 9ZM 333 Y Y )4 ) )  > . ?+nD$) ?83  _/9216'   /S 937T?/ VS933  `9G9 &!` )= ) )  o16*&51*& >1p1*+!D!3   !9E7FM )N )* )* + +  #*216 - !'S &qrs<Zt  ITJ 9ZM Z[9EcFM <Z[9E&FM <Z[9EBFM  L0'E!FS1$ 1>1p E(74FM ) )u $.   %S)?+2169+ EF!)*3 !LG c jE&7&&7 jF W &ZjE7&FM #3^_E`kG ( 4 KWbY`7FM Y  9216'1K  LG c jE(7 47 FM ) % % &. !"  #$%&"&&  '$%! (    )*+, - . /%0 1,%2%3 4    1$1%25167 121682+%9!: %25163   - . 1$1; #<<3 =    216*&51*& 1*>?@A3& B 2C /, - . !DE9F! 255EF GH IJK &LE&)7 &FM N    **&7?+*&&%? *+!O*&3 IJKLM  P2@&%1)%2%&Q*R ?8*+!DE9F!255 EF1S3 ITJKU%9?8!V ? -  &9E&)7&F W)XMY  P/&% Z[LE9FM    2]*P2@&%7?+'> &%1+?)3 ITJK #3^_E`aKWbY`7 E)(3=7NFFM 

Ngày đăng: 12/07/2014, 02:20

Từ khóa liên quan

Mục lục

  • Chapter 4 Delegate & Event

  • Nội dung

  • Delegate là gì

  • Delegate

  • Khai báo delegate

  • Khởi tạo biến delegate

  • Thực thi delegate

  • The type or signature of the method the delegate can point to

  • Slide 9

  • Biến delegate trong C#

  • Passing delegates to methods

  • Delegates in the .Net Framework

  • Slide 13

  • Multicast Delegates

  • Thực thi Multicast Delegate

  • Multicast delegate's invocation

  • Slide 17

  • Problem

  • Solution

  • Slide 20

Trích đoạn

Tài liệu cùng người dùng

Tài liệu liên quan