C#中Event事件的實(shí)現(xiàn)示例
什么是事件?
定義
- 事件是一種特殊的委托,用于在某一動(dòng)作發(fā)生時(shí)通知其他對(duì)象。
- 它允許一個(gè)對(duì)象向多個(gè)監(jiān)聽者廣播消息。
用途
- 實(shí)現(xiàn)發(fā)布-訂閱模式,主要用于解耦對(duì)象之間的交互,不同模塊能動(dòng)態(tài)響應(yīng)特定事件。
和委托關(guān)系
- 委托是事件的基礎(chǔ),它定義了事件處理器的簽名。事件是基于委托的封裝和擴(kuò)展。
事件的組成部分
- 事件聲明:在類中定義事件,通常使用event關(guān)鍵字。
- 事件觸發(fā):在某個(gè)動(dòng)作完成后,引發(fā)事件。
- 事件訂閱:外部對(duì)象注冊(cè)方法來(lái)響應(yīng)事件。
基本語(yǔ)法
1. 定義事件
public delegate void NotifyEventHandler(object sender, EventArgs e); public class Process { // 使用委托類型定義事件 public event NotifyEventHandler ProcessCompleted; public void StartProcess() { Console.WriteLine("Process started."); // 模擬過(guò)程完成后引發(fā)事件 OnProcessCompleted(EventArgs.Empty); } protected virtual void OnProcessCompleted(EventArgs e) { ProcessCompleted?.Invoke(this, e); } }
2. 訂閱和觸發(fā)事件
public class Program { public static void Main() { Process process = new Process(); // 訂閱事件 process.ProcessCompleted += Process_ProcessCompleted; process.StartProcess(); } private static void Process_ProcessCompleted(object sender, EventArgs e) { Console.WriteLine("Process completed event received."); } }
說(shuō)明:
- ProcessCompleted事件是在OnProcessCompleted方法中觸發(fā)的。
- Process_ProcessCompleted是事件處理器,當(dāng)事件被觸發(fā)時(shí)會(huì)被調(diào)用。
高級(jí)主題
內(nèi)置事件處理器
- EventHandler:這是C#提供的標(biāo)準(zhǔn)事件處理器委托類型,常用于無(wú)數(shù)據(jù)或簡(jiǎn)單數(shù)據(jù)的事件:
public event EventHandler ProcessCompleted;
- EventHandler<TEventArgs>:用于傳遞附加信息:
public class CustomEventArgs : EventArgs { public string Message { get; set; } } public event EventHandler<CustomEventArgs> ProcessCompletedWithMessage;
使用內(nèi)置EventHandler
public class SimpleProcess { public event EventHandler ProcessCompleted; public void CompleteProcess() { Console.WriteLine("Completing process..."); ProcessCompleted?.Invoke(this, EventArgs.Empty); } }
自定義事件參數(shù)
- 如果需要傳遞額外的數(shù)據(jù),可以創(chuàng)建自定義的事件參數(shù)類。
public class DownloadEventArgs : EventArgs { public string FileName { get; set; } public long FileSize { get; set; } } public class DownloadManager { public event EventHandler<DownloadEventArgs> DownloadCompleted; public void CompleteDownload(string fileName, long fileSize) { Console.WriteLine($"Download completed: {fileName}, Size: {fileSize}"); OnDownloadCompleted(new DownloadEventArgs { FileName = fileName, FileSize = fileSize }); } protected virtual void OnDownloadCompleted(DownloadEventArgs e) { DownloadCompleted?.Invoke(this, e); } }
事件的多播特性
事件是基于委托的,因此支持多播,即多個(gè)訂閱者可以同時(shí)接收同一事件通知。
public static void HandleSuccess(object sender, EventArgs e) { Console.WriteLine("Success handler executed."); } public static void Main() { SimpleProcess sp = new SimpleProcess(); // 多個(gè)方法訂閱同一事件 sp.ProcessCompleted += Process_ProcessCompleted; sp.ProcessCompleted += HandleSuccess; sp.CompleteProcess(); }
實(shí)踐習(xí)題
1.創(chuàng)建一個(gè)類DownloadManager,定義一個(gè)DownloadCompleted事件。在下載完成后觸發(fā)此事件,并在主程序中訂閱和處理該事件。
using System; public class DownloadManager { public event EventHandler DownloadCompleted; public void StartDownload() { Console.WriteLine("Starting download..."); // Simulate download completion OnDownloadCompleted(EventArgs.Empty); } protected virtual void OnDownloadCompleted(EventArgs e) { DownloadCompleted?.Invoke(this, e); } } public class Program { public static void Main() { DownloadManager dm = new DownloadManager(); // 訂閱事件 dm.DownloadCompleted += Download_Completed; dm.StartDownload(); } private static void Download_Completed(object sender, EventArgs e) { Console.WriteLine("Download completed event received."); } }
2.修改上述DownloadManager,使得DownloadCompleted事件攜帶下載的文件名和大小信息。
using System; public class DownloadEventArgs : EventArgs { public string FileName { get; set; } public long FileSize { get; set; } } public class DownloadManager { public event EventHandler<DownloadEventArgs> DownloadCompleted; public void StartDownload(string fileName, long fileSize) { Console.WriteLine($"Starting download of {fileName}..."); // Simulate download completion OnDownloadCompleted(new DownloadEventArgs { FileName = fileName, FileSize = fileSize }); } protected virtual void OnDownloadCompleted(DownloadEventArgs e) { DownloadCompleted?.Invoke(this, e); } } public class Program { public static void Main() { DownloadManager dm = new DownloadManager(); // 訂閱事件 dm.DownloadCompleted += Download_Completed; dm.StartDownload("example.txt", 1024); } private static void Download_Completed(object sender, DownloadEventArgs e) { Console.WriteLine($"Download completed: {e.FileName}, Size: {e.FileSize}"); } }
以上內(nèi)容涵蓋了如何定義、使用和擴(kuò)展事件。更多相關(guān)C# Event事件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WPF實(shí)現(xiàn)炫酷的界面交互效果的代碼詳解
在當(dāng)今競(jìng)爭(zhēng)激烈的軟件市場(chǎng)中,用戶界面的交互體驗(yàn)至關(guān)重要,一個(gè)擁有炫酷動(dòng)畫特效的應(yīng)用程序,不僅能吸引用戶的注意力,還能顯著提升用戶與界面的交互流暢度和愉悅感,本文將深入剖析WPF動(dòng)畫特效的各個(gè)方面,通過(guò)大量詳細(xì)的代碼示例和對(duì)關(guān)鍵概念的深入解釋2025-02-02C#利用TreeView控件實(shí)現(xiàn)目錄跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了C#潤(rùn)滑利用TreeView控件實(shí)現(xiàn)目錄跳轉(zhuǎn)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-07-07c# WPF中通過(guò)雙擊編輯DataGrid中Cell的示例(附源碼)
這篇文章主要介紹了c# WPF中通過(guò)雙擊編輯DataGrid中Cell的示例(附源碼),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03c# for循環(huán)中創(chuàng)建線程執(zhí)行問題
這篇文章主要介紹了有關(guān)c# for循環(huán)中創(chuàng)建線程執(zhí)行問題,下面文章將將以舉例的方式展開for循環(huán)中創(chuàng)建線程執(zhí)行問題的內(nèi)容,需要的朋友可以參考一下,希望對(duì)你有所幫助2021-11-11Unity多語(yǔ)言轉(zhuǎn)換工具的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Unity多語(yǔ)言轉(zhuǎn)換工具的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06Unity Shader相交算法實(shí)現(xiàn)簡(jiǎn)易防能量盾
這篇文章主要為大家詳細(xì)介紹了Unity Shader相交算法實(shí)現(xiàn)簡(jiǎn)易防能量盾,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04