C# 實現自定義事件


代碼中實現了三個自定義事件,分別為自定義事件、自定義事件及自定義參數、使用Action自定義事件。

 

using  System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AppMain
{
    /// <summary>
    
/// 入口
    
/// </summary>
    public class RunEventDemo
    {
        #region 用Action定義事件測試
        public void RunProgramThree()
        {
            EventDemoThree edth = new EventDemoThree();
            edth.CustomerEvent += new Action<object, EventArgs>(edth_CustomerEvent);
            //觸發事件
            edth.OnCustomerEvent();
        }

        void edth_CustomerEvent(object obj, EventArgs e)
        {
            Console.WriteLine("已調用Actiion執行的事件");
        }
        #endregion

        #region 使用自定義事件參數事件測試
        public void RunProgramTwo()
        {
            EventDemoTwo edt = new EventDemoTwo();
            edt.CustomerEvent += new EventDemoTwo.CustomerEventHander(edt_CustomerEvent);

            //創建事件參數
            CustomerEventArgs cea = new CustomerEventArgs();
            cea.CustomerMsg = " test";

            //觸發事件
            edt.OnCustomerEvent(cea);
        }

        void edt_CustomerEvent(object sender, CustomerEventArgs e)
        {
            Console.WriteLine("你傳入的參數值為:" + e.CustomerMsg);
        }


        #endregion

        #region 無參的自定義事件測試
        public void RunProgramOne()
        {
            //事件接收者
            EventDemoOne ed = new EventDemoOne();
            //4.注冊事件處理程序
            ed.customerEvent += new EventDemoOne.CustomerEventHander(RunDemo_customerEvent);
            //6.調用事件
            ed.OnEvent();
        }
        //5.編寫事件處理程序
        public void RunDemo_customerEvent(object sender, EventArgs e)
        {
            Console.WriteLine("test");
        }
        #endregion
    }

     

 

#region無參的自定義事件

    /// <summary>
    
/// 無參的自定義事件
    
/// </summary>
    public class EventDemoOne
    {
        //1.聲明關於事件的委托;
        public delegate void CustomerEventHander(object sender, EventArgs e);
        //2.聲明事件;   
        public event CustomerEventHander customerEvent;
        //3.編寫引發事件的函數;
        public void OnEvent()
        {
            if (this.customerEvent != null)
            {
                this.customerEvent(thisnew EventArgs());
            }
        }
    }
    #endregion

     

#region自定義事件,使用自定義事件參數
    /// <summary>
    
/// 自定義事件參數
    
/// </summary>
    public class CustomerEventArgs : EventArgs
    {
        public string CustomerMsg { getset; }
    }

    public class EventDemoTwo
    {
        public delegate void CustomerEventHander(object sender, CustomerEventArgs e);
        public event CustomerEventHander CustomerEvent;
        //3.編寫引發事件的函數,注意多了個參數;
        public void OnCustomerEvent(CustomerEventArgs e)
        {
            this.CustomerEvent(this, e);
        }
    }
    #endregion

     

#region使用Action自定義事件
    public class EventDemoThree
    {
        public event Action<object, EventArgs> CustomerEvent;
        public void OnCustomerEvent()
        {
            this.CustomerEvent(thisnew EventArgs());
        }
    }
    #endregion

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM