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

首頁 > 學院 > 開發設計 > 正文

觸發事件,檢查是否注冊過事件的方法

2019-11-17 02:38:28
字體:
來源:轉載
供稿:網友

觸發事件,檢查是否注冊過事件的方法

下面的代碼,會發現事件沒有被注冊

 class PRogram    {        static void Main(string[] args)        {            EventCheck check = new EventCheck();            check.OnDataArrived(new DataArrivedEventArgs() { data="Hello World!"});            Console.ReadKey();        }        static void check_DataArrived(object sender, DataArrivedEventArgs e)        {            Console.WriteLine(e.data);        }    }    class EventCheck    {        //第三步 use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.        //To associate the event with the method that will handle the event, add an instance of the delegate to the event.         //The event handler is called whenever the event occurs, unless you remove the delegate.        internal event DataArrivedEventHandler DataArrived;                //第四步        internal void OnDataArrived(DataArrivedEventArgs e)        {            try            {                //DataArrivedEventHandler handler = DataArrived;                //if (handler != null)                //{                //    handler(this, e);                //}                DataArrived += EventCheck_DataArrived;                if (DataArrived != null)                {                    DataArrived -= EventCheck_DataArrived;                    DataArrived(this, e);                }            }            catch (Exception ex)            {                Console.WriteLine("遇到異常{0}", ex.Message);            }        }        void EventCheck_DataArrived(object sender, DataArrivedEventArgs e)        {            Console.WriteLine(e.data);        }    }    //第二步   Represents the method that will handle an event when the event provides data.    //This method's first parameter is of type Object and refers to the instance that raises the event.     //Its second parameter is derived from type EventArgs and holds the event data.    //If the event does not generate event data, the second parameter is simply the value of the EventArgs.Empty field.     //Otherwise, the second parameter is a type derived from EventArgs and supplies any fields or properties needed to hold the event data.    delegate void DataArrivedEventHandler(object sender,DataArrivedEventArgs e);  //正式寫代碼的時候,可以使用EventHandler<TEventArgs>泛型委托    //第一步       //create a custom event data class, create a class that derives from the EventArgs class and provide the properties to store the necessary data.     //The name of your custom event data class should end with EventArgs.    class DataArrivedEventArgs : EventArgs    {       internal string data;    }

修改第四步的代碼,先將事件復制到臨時變量里面,然后通過這個臨時變量來處理,就不會有問題

//第四步        internal void OnDataArrived(DataArrivedEventArgs e)        {            try            {                //DataArrivedEventHandler handler = DataArrived;                //if (handler != null)                //{                //    handler(this, e);                //}                DataArrived += EventCheck_DataArrived;                DataArrivedEventHandler handler = DataArrived;                if (handler != null)                {                    DataArrived -= EventCheck_DataArrived;                    handler(this, e);                }            }            catch (Exception ex)            {                Console.WriteLine("遇到異常{0}", ex.Message);            }        }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 印江| 通化市| 英德市| 靖西县| 工布江达县| 克什克腾旗| 尼玛县| 华安县| 洛阳市| 剑川县| 台湾省| 龙岩市| 昭平县| 临海市| 盐边县| 永平县| 离岛区| 化隆| 同德县| 甘孜县| 孙吴县| 梁河县| 正定县| 泰兴市| 白朗县| 晋江市| 澄城县| 陕西省| 纳雍县| 吴堡县| 凌云县| 海南省| 正安县| 襄城县| 洪洞县| 尖扎县| 安国市| 如皋市| 灌阳县| 盐池县| 邯郸县|