麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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

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);            }        }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色羞羞 | 一级大片久久 | xxxx69hd一hd72| 一级免费黄色免费片 | 成人小视频在线播放 | 91久久91久久精品免观看 | 成人短视频在线播放 | 欧美一级美国一级 | 中国免费一级毛片 | 中文字幕精品在线视频 | 欧美日韩在线视频一区 | 深夜福利视频免费观看 | 欧美大片一级毛片 | 国产乱淫av片免费 | 国产精品久久久久久久久久尿 | 成人在线观看一区二区 | 毛片福利| 久久久免费电影 | 成人国产在线看 | 久久久久久久免费视频 | 久久久国产精品网站 | 成人午夜免费在线视频 | 看一级毛片 | 欧美成年私人网站 | 国内久久久久 | 性生活视频软件 | 国产精品一品二区三区四区18 | 午夜精品老牛av一区二区三区 | 成人激情综合网 | 成人毛片100部| 久久综合入口 | 亚洲av一级毛片特黄大片 | av在线播放亚洲 | 久久久久99精品 | 国产毛毛片一区二区三区四区 | 亚洲视频观看 | 精品亚洲夜色av98在线观看 | 国产电影av在线 | 欧美日韩大片在线观看 | 欧美性生交大片 | 日韩视频一区二区三区在线观看 |