本人一直認(rèn)為AS3的事件處理機(jī)制很是給力 , 今天鼓搗了出來(lái)并完美得通過(guò)了測(cè)試。在AS3中使用函數(shù)addEventListener添加事件偵聽(tīng),用removeEventListener移除事件偵聽(tīng)。著用封裝的一個(gè)類庫(kù)可以徹底地終結(jié)消息傳遞中無(wú)規(guī)則,無(wú)規(guī)律的混亂狀態(tài),從而達(dá)到代碼邏輯清晰性。改起來(lái)也相當(dāng)簡(jiǎn)單(做過(guò)程序員的都懂)。
成都創(chuàng)新互聯(lián)公司專注于泊頭企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,成都做商城網(wǎng)站。泊頭網(wǎng)站建設(shè)公司,為泊頭等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站建設(shè),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)關(guān)于此類庫(kù)的實(shí)現(xiàn)原理 , 其實(shí)使用的是委托(delegate),讓偵聽(tīng)函數(shù)(觀察者)掛載到此委托上,當(dāng)然消息有不同的類型,如windows系統(tǒng)中有單擊,雙擊,右擊等不同的事件類型,在這個(gè)類庫(kù)里面都有實(shí)現(xiàn)。
首先,需要指出:
IEventType ( 所有事件類型的接口 )
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MsgEventLib.com { public interface IEventType { /// <summary> /// 類的類型 /// </summary> Type EventMainType { get; } String GetEventTypeName(string type); } }
思想:事件類型以 類的Type.Name + "_" + 事件名稱 。 以GetEventTypeName方法實(shí)現(xiàn)。
傳遞的消息體:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MsgEventLib.com { public class BaseEvent { public BaseEvent(string eventType, object sender, object msg) { this.eventType = eventType; this.sender = sender; this.msg = msg; } private string @eventType; public string EventType { set { this.eventType = value; } get { return this.eventType; } } private object @sender; public Object Sender { set { this.sender = value; } get { return this.sender; } } private object @msg; public object Msg { set { this.msg = value; } get { return this.msg; } } } }
其中:eventType為事件類型名稱 GetEventTypeName , Sender為發(fā)送者 , Msg為消息體
事件偵聽(tīng)管理器的實(shí)現(xiàn)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MsgEventLib.com { public sealed class EventListener : IEventListener { private static IEventListener @instance; public static IEventListener Instance { get { if (instance == null) instance = new EventListener(); return instance; } } private Dictionary<string, DeListener> @listeners; private EventListener() { @listeners = new Dictionary<string, DeListener>(); } public void AddEventListener(string type, DeListener myListener) { if (!@listeners.ContainsKey(type)) @listeners.Add(type, myListener); else @listeners[type] += myListener; } public void RemoveEventListener(string type, DeListener myListener) { if (@listeners.ContainsKey(type)) { @listeners[type] -= myListener; if (@listeners[type] == null) @listeners.Remove(type); } } public void RemoveAllEventListener() { if (@listeners != null && @listeners.Count > 0) { List<string> keys = new List<string>(@listeners.Keys);//獲得所有的鍵值 for (int j = 0; j < keys.Count; j++) { if (@listeners[keys[j]] != null) { Delegate[] des = @listeners[keys[j]].GetInvocationList(); if (des != null && des.Length > 0) { for (int i = 0; i < des.Length; i++) { @listeners[keys[j]] -= des[i] as DeListener; } } } @listeners.Remove(keys[j]); } } } public DeListener GetDeListenerByType(string type) { if (@listeners != null && @listeners.ContainsKey(type)) { return @listeners[type]; } return null; } public void Destory() { this.RemoveAllEventListener(); if (@instance != null) @instance = null; } } }
值得注意的是 , 字典 : key為事件類型(GetEventTypeName) , value為委托(可能有多個(gè)掛載/觀察者)
關(guān)于事件發(fā)送者(主題)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MsgEventLib.com { public sealed class EventDispacterManager : IEventDispacterManager { private static IEventDispacterManager @instance; public static IEventDispacterManager Instance { get { if (instance == null) instance = new EventDispacterManager(); return instance; } } public void Dispatch(string type , BaseEvent @myevent) { DeListener target = EventListener.Instance.GetDeListenerByType(type); if (target != null) { target(@myevent); } } } }
測(cè)試的結(jié)果
1 , 事件類型
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MsgEventLib.com; namespace TestA.com { public class MyEventType : IEventType { private static MyEventType @instance; public static MyEventType Instance { get { if (@instance == null) @instance = new MyEventType(); return @instance; } } public Type EventMainType { get { return typeof(MyEventType); } } public string GetEventTypeName(string type) { return this.EventMainType.Name + "_" + type; } public static string CLOSE_WINDOWS = "CLOSE_WINDOWS"; public static string OTHRT_TYPE = "OTHRT_TYPE"; } }
2 , 3個(gè)類(偵聽(tīng)著)
①:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MsgEventLib.com; namespace TestA.com { public sealed class Listeners { public Listeners() { string type = MyEventType.Instance.GetEventTypeName(MyEventType.CLOSE_WINDOWS); EventListener.Instance.AddEventListener(type, this.Li); } private void Li(BaseEvent e) { Console.WriteLine("Listeners 觸發(fā)了Event {0} ", e.Msg); } } }
②:
using MsgEventLib.com; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestA.com { public sealed class Lis2 { public Lis2() { string type = MyEventType.Instance.GetEventTypeName(MyEventType.CLOSE_WINDOWS); EventListener.Instance.AddEventListener(type, this.Li); } private void Li(BaseEvent e) { Console.WriteLine("Lis2 觸發(fā)了Event {0} ", e.Msg); } public void RemoveLis() { string type = MyEventType.Instance.GetEventTypeName(MyEventType.CLOSE_WINDOWS); EventListener.Instance.RemoveEventListener(type, this.Li); Console.WriteLine("Lis2 移除了事件偵聽(tīng)!"); } } }
③:
using MsgEventLib.com; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestA.com { public sealed class Lis3 { public Lis3() { string type = MyEventType.Instance.GetEventTypeName(MyEventType.OTHRT_TYPE); EventListener.Instance.AddEventListener(type, this.Li); } private void Li(BaseEvent e) { Console.WriteLine("Lis3 觸發(fā)了Event {0} ", e.Msg); } } }
事件發(fā)送者
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MsgEventLib.com; namespace TestA.com { public sealed class Dispatch { public void Dis() { string type = MyEventType.Instance.GetEventTypeName(MyEventType.CLOSE_WINDOWS); EventDispacterManager.Instance.Dispatch(type, new BaseEvent(type, this, "Event_關(guān)閉你的窗口")); } } }
測(cè)試:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestA.com;
namespace TestA
{
public class Program
{
static void Main(string[] args)
{
Dispatch di = new Dispatch();
Listeners li = new Listeners();
Lis2 li2 = new Lis2();
Lis3 li3 = new Lis3();
di.Dis();
li2.RemoveLis();
di.Dis();
Console.Read();
}
}
}
結(jié)果:
因?yàn)長(zhǎng)is3偵聽(tīng)的不是事件CLOSE_WINDOWS , 既不會(huì)觸發(fā) 。 因?yàn)長(zhǎng)is2移除了事件,所以第二次不會(huì)觸發(fā)。
這只是部分大碼。詳細(xì)請(qǐng)見(jiàn)附件。
附件:http://down.51cto.com/data/2366478另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站標(biāo)題:C#消息事件封裝-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)路徑:http://chinadenli.net/article8/dhieop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、外貿(mào)網(wǎng)站建設(shè)、定制網(wǎng)站、品牌網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容