c#中事件模型的模拟

  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底层调用通知应用程序的吧

你可能感兴趣的:(c#中事件模型的模拟)