c#中事件模型的類比

來源:互聯網
上載者:User
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CSharp基礎
  6. {
  7.     public delegate void ActionEventHandler(object sender, ActionCancelEventArgs args);
  8.     class 事件模型
  9.     {
  10.         public static void Main()
  11.         {
  12.             MyButton but = new MyButton();
  13.             but.Action += new ActionEventHandler(but_Action);
  14.             but.OnClickMyButtonEvent();
  15.             Console.ReadLine();
  16.             
  17.         }
  18.         static void but_Action(object sender, ActionCancelEventArgs args)
  19.         {
  20.             args.Cancel = true;
  21.         }
  22.     }
  23.     public class ActionCancelEventArgs : System.ComponentModel.CancelEventArgs
  24.     { 
  25.     }
  26.     class MyButton
  27.     {
  28.         public  event ActionEventHandler Action ;
  29.         //本方法本來是由windows在滑鼠點擊時調用
  30.         public void OnClickMyButtonEvent()
  31.         {
  32.             if (Action != null)
  33.             {
  34.                 ActionCancelEventArgs args = new ActionCancelEventArgs() ;
  35.                 Action(this, args);
  36.              
  37.                 if (args.Cancel)
  38.                 {
  39.                     Console.WriteLine( "事件被使用者取消");
  40.                 }
  41.             }
  42.         }
  43.     }
  44. }
  45. 你們看看我類比的這個事件模型對不對?
  46. 是不是這個意思,主要是OnClickMyButtonEvent這個方法,我想應該是windows底層調用通知應用程式的吧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.