国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#自定義事件監聽實現方法

2020-01-24 01:30:32
字體:
來源:轉載
供稿:網友

本文實例講述了C#自定義事件監聽實現方法。分享給大家供大家參考。具體實現方法如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApp{ /// <summary> /// 定義事件 /// </summary> class CustomEvent {  /// <summary>  /// 定義委托  /// </summary>  /// <param name="sender"></param>  /// <param name="e"></param>  public delegate void UserRequest(object sender, EventArgs e);  /// <summary>  /// 此委托類型的事件  /// </summary>  public event UserRequest OnUserRequest;  public CustomEvent()  {   UserEventMonitor uem = new UserEventMonitor(this);  }  public void DoRun()  {   bool flag = false;   do   {    Console.WriteLine();    Console.WriteLine("請輸入:");    string result = Console.ReadLine();    if (result == "1")    {     if (OnUserRequest != null)      OnUserRequest(this, new EventArgs());    }   } while (!flag);  } } /// <summary> /// 事件監聽 /// </summary> class UserEventMonitor {  public UserEventMonitor(CustomEvent cem)  {   // cem.OnUserRequest += ShowMessage;   cem.OnUserRequest += delegate   {    Console.WriteLine("hello word!!");   };  }  public void ShowMessage(object sender, EventArgs e)  {   Console.WriteLine("hello word!!");  } } /// <summary> /// 調用類 /// </summary> public class Run {  static void Main(string[] args)  {   CustomEvent cem = new CustomEvent();   cem.DoRun();   Console.ReadLine();  } }}

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 剑川县| 云阳县| 东乡族自治县| 台前县| 宜章县| 郯城县| 拉萨市| 中方县| 林西县| 京山县| 呼玛县| 深水埗区| 二手房| 大英县| 独山县| 璧山县| 齐齐哈尔市| 星座| 新化县| 乳源| 阳谷县| 瑞昌市| 瑞昌市| 手机| 郑州市| 万宁市| 德兴市| 夏河县| 锡林浩特市| 昌都县| 刚察县| 沾益县| 揭东县| 绥滨县| 龙州县| 库伦旗| 乳山市| 藁城市| 乌兰察布市| 乐安县| 阜平县|