SendReceiveTest.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" preinitialize="onInit()" layout="vertical">
<mx:Script>
<![CDATA[
private function onInit():void {
EventUtil.register("event3", onReceive);
}
private function onReceive(msg:Message):void {
var from:String;
if (msg.source==this) {
from=" self";
} else {
from=" other";
}
output.text="Event event3 received, id="+msg.data.id+" msg="+msg.data.msg;
}
private function onSend():void {
var e:Message=new Message("event3",this, {id:10, msg:"Hello"});
EventUtil.dispatch(e);
}
]]>
</mx:Script>
<mx:Button label="Send" click="onSend()"/>
<mx:Text id="output" text="output" width="300" height="100"/>
</mx:Application>
EventUtil.as
//EventUtil.as
package
{
import flash.events.Event;
import mx.core.Application;
public class EventUtil
{
public static function register(eventName:String, func:Function):void {
Application.application.stage.addEventListener(eventName,func);
}
public static function dispatch(event:Event):void {
Application.application.stage.dispatchEvent(event);
}
}
}
Message.as
//Message.as
package
{
import flash.events.Event;
public class Message extends Event
{
public var source:Object;
public var data:Object;
public function Message(type:String, sourceObject:Object, dataObject:Object)
{
super(type);
this.source=sourceObject;
this.data=dataObject;
}
}
}