Flex中所有的组件都间接继承自EventDispatcher,通过查看Flex API,了解一下这个类中的方法。 如图所见,该类包含了派发事件、监听事件、移出事件等方法,那么通过这个类就可以实现本文开篇所提出的那种情况,分析一下,监听事件的对象和派发事件的对象必须是同一个对象,这样事件才能被捕获,所以我们需要写一个单例的类,并且组合EventDispatcher,可以满足需求。
package events
{
import events.MyEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import mx.core.Singleton;
public class TestEventDispatcher
{
private static var _inst:MyEventDispatcher;
private var eventDispatcher:IEventDispatcher;
public function MyEventDispatcher(singleton:Singleton)
{
if(singleton == null)
{
throw new Error("Create MyEventDispatcher Error!");
}
eventDispatcher = new EventDispatcher();
}
public static function getInstance():MyEventDispatcher
{
if (!_inst)
{
_inst = new MyEventDispatcher(new Singleton);
}
return _inst;
}
public function addEventListener(type:String,listener:Function,useCapture:Boolean=false,pririty:int=0,useWeakReference:Boolean=true):void
{
eventDispatcher.addEventListener(type,listener,useCapture,pririty,useWeakReference);
}
public function removeEventListener(type:String,priority:int=0,useWeakReference:Boolean=true):void
{
eventDispatcher.removeEventListener(type,useCapture);
}
public function dispatchEvent(event:MyEvent):Boolean
{
return eventDispatcher.dispatchEvent(event);
}
public function hasEventListener(type:String):Boolean
{
return eventDispatcher.hasEventListener(type);
}
public function willTrigger(type:String):Boolean
{
return eventDispatcher.willTrigger(type);
}
}
}
class Singleton {}
events/MyEvent.as,我们可以在该类中加入一个dispatch方法,创建完事件之后可以派发,代码如下:
/**
* 派发事件
* @return
*/
public function dispatch():Boolean
{
return MyEventDispatcher.getInstance().dispatchEvent(this);
}
这样,创建完自定义事件之后,就可以直接派发事件了,那么使用起来就很方便了,下面是具体使用代码,一个应用中有两个自定义组件,组件一中有个TextArea,组件二中有个按钮,点击按钮,将数据传递到TextArea中。
EventTest.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
xmlns:components="components.*">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<components:component1 />
<components:component2 />
</s:Application>
components/component1.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import events.MyEventDispatcher;
import events.MyEvent;
import mx.events.FlexEvent;
protected function creationCompleteHandler(event:FlexEvent):void
{
MyEventDispatcher.getInstance().addEventListener(MyEvent.SHOWINFO,showInfo);
}
protected function showInfo(event:MyEvent):void
{
textArea.text = event.data;
}
]]>
</fx:Script>
<s:TextArea id="textArea" />
</s:Group>
components/component2.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import events.MyEvent;
import events.MyEventDispatcher;
protected function buttonClickHandler(event:MouseEvent):void
{
var myEvent:MyEvent = new MyEvent(MyEvent.SHOWINFO);
myEvent.data = "哈哈";
myEvent.dispatch();
}
]]>
</fx:Script>
<s:Button label="显示内容" click="buttonClickHandler(event)"/>
</s:Group>