You can manually dispatch events using a component instances dispatchEvent() method. All components that extend UIComponent have this method. The method is inherited from the EventDispatcher class, which UICom-ponent extends.
The syntax for the dispatchEvent() method is as follows:
你可以使用组件的实例方法dispatchEvent() 来手动派发事件。所有的继承UIComponent的组件都有此方法,该方法继承自EventDispatcher类。
dispatchEvent() 语法如下:
objectInstance.dispatchEvent(event:Event):Boolean
When dispatching an event, you must create a new Event object. The syntax for the Event object constructor is as
follows:
当你派发一个事件时,你必须创建一个Event对象,Event的构造函数如下:
Event(event_type:String, bubbles:Boolean, cancelable:Boolean)
The event_type parameter is the type property of the Event object. The bubbles and cancelable parameters
are optional and both default to false.
event_type参数是事件类型。bubbles and cancelable参数是可选的,默认都是false。
You can use the dispatchEvent() method to dispatch any event you want, not just a custom event. You can dispatch a Button controls click event, even though the user did not click a Button control, as in the following example:
你可以使用 dispatchEvent() 来派发你想派发的任何事件,不局限于自定义的事件。你可以派发一个Button控件的click事件,即使用户并没有单击Button控件,如下例子:
<?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>
You can also manually dispatch an event in an MXML tag. In the following example, moving the mouse pointer
over the button triggers the buttons click event:
你也可以在MXML标签中手动派发事件。在下面的例子中,当鼠标划过button按钮将会触发button的click事件。
<?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>
Your Flex application is not required to handle the newly dispatched event. If you trigger an event that has no listeners, Flex ignores the event.
Flex应用不一定必须处理新派发的事件,如果你触发一个事件而这个事件没有监听者,那么Flex将会忽略它。
You can set properties of the Event object in ActionScript, but you cannot add new properties because the object is not dynamic. The following example intercepts a click event. It then creates a new MouseEvent object and dispatches it as a doubleClick event. In addition, it sets the value of the shiftKey property of the MouseEvent object to true, to simulate a Shift-click on the keyboard.
你可以在Actionscript中设置Event对象的属性值,但是你不能给它添加新的属性因为该对象不是动态的。下面的例子拦截了一个click事件,
然后创建了一个新的MouseEvent对象把它作为doubleClick事件派发。此外,它还把MouseEvent的shiftKey属性设置为true来模拟键盘上的Shift-click。
<?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>
If you want to add custom properties to an Event object, you must extend the Event object and define the new properties in your own custom class. You can then manually dispatch your custom events with the dispatchEvent() method, as you would any event.
If you create a custom ActionScript class that dispatches its own events but does not extend UIComponent, you can extend the flash.events.EventDispatcher class to get access to the addEventListener(), removeEventListener(), and dispatchEvent() methods.
如果你想为一个Event对象添加上自定义属性,就必须得继承Event类,在你的定义的新类中定义新的属性。然后你可以用dispatchEvent()方法手动派发你的自定义事件。
如果你想创建一个自定义的AS类来派发它自己的事件,而该类并没有继承UIComponent你可以继承flash.events.EventDispatcher 类来使用 addEventListener(),
removeEventListener(), and dispatchEvent() methods.方法。
For more information on creating custom classes, see Creating and Extending Adobe Flex 3 Components.