一、   手动触发事件<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

你可以通过使用组件实例的dispatcheEvent()方法来手动的调度事件。所有继承自UIComponent类的组件都拥有这个方法。这个方法继承自EventDispatcher类。

 

dispatchEvent()方法的语法如下所示:

 

objectInstance.dispatchEvent(event:Event):Boolean

 

当调度一个事件的时候,你必须创建一个新的Event对象。Event对象的构造函数如下:

 

Event(event_type:String, bubbles:Boolean, cancelable:Boolean)

 

event_type参数是Event对象的type属性。bubblescancelable参数都是可选的并且默认为false

 

你可以使用dispatchEvent()方法调度你需要的任意事件,而不仅仅是用户事件。不管用户是否点击了按钮控件,你都可以调度一个按钮控件的click事件。如下所示:

 

<?xml version="1.0"?>

<!-- events/DispatchEventExample.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

initialize="createListener(event);">

<mx:Script><![CDATA[

import mx.controls.Alert;

private function createListener(e:Event):void {

b1.addEventListener(MouseEvent.MOUSE_OVER, myEventHandler);

b1.addEventListener(MouseEvent.CLICK, myClickHandler);

}

private function myEventHandler(e:Event):void {

var result:Boolean = b1.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true,

false));

}

private function myClickHandler(e:Event):void {

Alert.show("The event dispatched by the MOUSE_OVER was of type '" + e.type +

"'.");

}

]]></mx:Script>

<mx:Button id="b1" label="Click Me"/>

</mx:Application>

 

你也可以在MXML标签中手动调度事件。在下面的例子中,将鼠标指针移出按钮将触发按钮的点击事件:

 

<?xml version="1.0"?>

<!-- events/DispatchEventExampleInline.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

initialize="createListener(event);">

<mx:Script><![CDATA[

import mx.controls.Alert;

private function createListener(e:Event):void {

b1.addEventListener(MouseEvent.CLICK, myClickHandler);

}

private function myClickHandler(e:Event):void {

Alert.show("The event dispatched by the MOUSE_OVER was of type '" + e.type +

"'.");

}

]]></mx:Script>

<mx:Button id="b1"

label="Click Me"

mouseOver="b1.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));"

/>

</mx:Application>

 

你的Flex程序并非必须处理每个新调度的事件。如果你触发了一个没有监听器的事件,那么Flex将忽略这个事件。

 

你可以在ActionScript中设置Event对象的属性,但是你不能为Event对象添加新的属性,因为Event对象不是动态的。下面的例子拦截一个点击事件,然后创建一个新的MouseEvent对象,并且将它作为一个鼠标双击事件进行调度。它将MouseEventshiftKey属性设置为true,来模拟单击键盘上的Shift键。

 

<?xml version="1.0"?>

<!-- events/DispatchCustomizedEvent.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

creationComplete="addListeners()">

<mx:Script><![CDATA[

private function customLogEvent(e:MouseEvent):void {

l1.text = String(e.currentTarget.id);

l2.text = String(e.type);

l3.text = String(e.shiftKey);

// Remove current listener to avoid recursion.

e.currentTarget.removeEventListener("doubleClick", customLogEvent);

}

private function handleEvent(e:MouseEvent):void {

// Add new handler for custom event about to be dispatched.

e.currentTarget.addEventListener("doubleClick", customLogEvent);

// Create new event object.

var mev:MouseEvent = new MouseEvent("doubleClick");

// Customize event object.

mev.shiftKey = true;

// Dispatch custom event.

e.currentTarget.dispatchEvent(mev);

}

private function addListeners():void {

b1.addEventListener("click",handleEvent);

b2.addEventListener("click",handleEvent);

}

]]></mx:Script>

<mx:Button id="b1" label="Click Me (b1)"/>

<mx:Button id="b2" label="Click Me (b2)"/>

<mx:Form>

<mx:FormItem label="Current Target:">

<mx:Label id="l1"/>

</mx:FormItem>

<mx:FormItem label="Event Type:">

<mx:Label id="l2"/>

</mx:FormItem>

<mx:FormItem label="Shift Key Pressed:">

<mx:Label id="l3"/>

</mx:FormItem>

</mx:Form>

</mx:Application>

 

如果你想为Event对象添加用户自定义属性,那么你必须继承Event对象并且在你的用户自定义类中定义新的属性。然后就可以使用dispatchEvent()手动触发你的用户自定义事件。

 

如果你创建了一个用户自定义ActionScript类来调度它自己的事件,但它又没有继承UIComponent,你可选择继承flash.events.EventDispatcher类来获得addEventLIstener()removeEventListener()dispatchEvent()方法的访问。