首页 编程 正文

FLEX 事件机制-自定义事件介绍_Flex_

2023-05-26 01:41:37 65
最近在做地图JS和FLEX的封装工作,flex封装需要用到自定义事件这方面知识,在这里就简单介绍一下FLEX自定义事件。

自定义事件必须注意几点:

(1)自定义事件通过继承Event类来实现。

(2)自定义事件不可以自己触发,只能有系统事件派发。

首先我们新建一个ActionScript 类,这个类继承flash.events.Event。
复制代码 代码如下:

package
{
import flash.events.Event;

public class MyEvent extends Event
{
public static const myEvent:String="myEvent";

public var data:*;

public function MyEvent(type:String,bubbles:Boolean=false,cancellabel:Boolean=false,data:* = null)
{
super(type,false,false);
this.data = data;
}

override public function clone():Event{
return new MyEvent(myEvent,false,false,data);
}

override public function toString():String{

return formatToString("MyEvent");
}
}
}

创建一个组件来监听事件,实现数据的接受。

component.mxml
复制代码 代码如下:


xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" initialize="init()">





import mx.controls.Alert;
import mx.events.FlexEvent;
public var ff:FlexMyEvent;

public function init():void{

ff = this.parentDocument as FlexMyEvent;
ff.addEventListener(MyEvent.myEvent,myEventHandler);//监听事件

}
public function myEventHandler(eve:Object):void{

inpu.text = eve.data;
Alert.show("事件触发,"+eve.data);
}

]]>



















主程序

FlexMyEvent.mxml
复制代码 代码如下:


xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:comp = "*"
width="100%" height="100%">



import flash.external.ExternalInterface;
import flash.system.Security;

import mx.controls.Alert;
import mx.events.FlexEvent;
public var callBackInfo:String = "";
public function doSend():void{

callBackInfo = input.text;
var myEve:MyEvent = new MyEvent(MyEvent.myEvent,false,false,callBackInfo);//派发事件 myEve.data is Object;
this.dispatchEvent(myEve);

}

]]>



























-六神源码网 -六神源码网