C#自定義事件監(jiān)聽實現(xiàn)方法
更新時間:2015年08月22日 17:15:58 作者:我心依舊
這篇文章主要介紹了C#自定義事件監(jiān)聽實現(xiàn)方法,涉及C#事件監(jiān)聽的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#自定義事件監(jiān)聽實現(xiàn)方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
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> /// 事件監(jiān)聽 /// </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> /// 調(diào)用類 /// </summary> public class Run { static void Main(string[] args) { CustomEvent cem = new CustomEvent(); cem.DoRun(); Console.ReadLine(); } } }
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
C#控制Excel Sheet使其自適應(yīng)頁寬與列寬的方法
這篇文章主要介紹了C#控制Excel Sheet使其自適應(yīng)頁寬與列寬的方法,涉及C#操作Excel的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06C#通過rabbitmq實現(xiàn)定時任務(wù)(延時隊列)
工作中經(jīng)常會有定時任務(wù)的需求,常見的做法可以使用Timer、Quartz、Hangfire等組件,本文使用C#通過rabbitmq實現(xiàn)定時任務(wù)(延時隊列),感興趣的可以了解一下2021-05-05Unity?百度AI實現(xiàn)Logo商標(biāo)識別
本文主要介紹了Unity實現(xiàn)檢測和識別圖片中的品牌LOGO信息。即對于輸入的一張圖片(可正常解碼,且長寬比適宜),輸出圖片中LOGO的名稱、位置和置信度。需要的可以參考一下2022-01-01Unity打開淘寶app并跳轉(zhuǎn)到商品頁面功能的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于如何利用Unity打開淘寶app并跳轉(zhuǎn)到商品頁面功能的相關(guān)資料,這個功能目前在網(wǎng)上找不到相關(guān)的解決方法,所以自己寫了出來,需要的朋友可以參考下2021-07-07C# 通過NI-VISA操作Tektronix TBS 2000B系列示波器的實現(xiàn)步驟
這篇文章主要介紹了C# 通過NI-VISA操作Tektronix TBS 2000B系列示波器的實現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02