Flex Event标签

Using the Event metadata tag
You use the [Event] metadata tag to define events dispatched by a component so that the Flex compiler can recognize
them as MXML tag attributes in an MXML file. You add the [Event] metadata tag in one of the following locations:
ActionScript components Above the class definition, but within the package definition, so that the events are bound
to the class and not a particular member of the class.
MXML components In the <mx:Metadata> tag of an MXML file.

 

Demo:

定义MyEvent extends Event

 

ActionScript 3语言:
package event
{
import flash.events.Event;

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

public function MyEvent( type : String , bubbles : Boolean = false , cancelable : Boolean = false)
{
super( type , bubbles , cancelable);
}
}
}

 

 

定义MyButton extends Button

 

MXML语言:
<?xml version="1.0" encoding="utf-8"?>
<s:Button xmlns:fx= "http://ns.adobe.com/mxml/2009"
xmlns:s= "library://ns.adobe.com/flex/spark"
xmlns:mx= "library://ns.adobe.com/flex/halo"
click= "dispatchEvent(new MyEvent(MyEvent.EVENT_TEST));" >
<fx:Script>
<![CDATA[
import event.MyEvent;
]]>
</fx:Script>
<fx:Metadata>
<!--在Metadata标签下定义Event编译期间检查-->
[Event(name ="EventTest",type ="event.MyEvent")]
</fx:Metadata>
</s:Button>

 

 

Main App

 

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/halo"
xmlns:my= "button.*"
minWidth= "1024"
minHeight= "768" >
<fx:Script>
<![CDATA[
import event.MyEvent;
/**
* 定义Event的函数监听方式
* 在my2 的 EventTest="handleEvent(event)" 相当于对于该Event注册了一个Listiner
* 消息监听函数为handleEvent(event)
*/
private function handleEvent( e : MyEvent ): void { trace( e); }
]]>
</fx:Script>
<!--可以直接定义EventTest="myTa.text='got Event';" 及在Event触发式执行myTa.text='got Event';-->
<my:MyButton id= "my1" EventTest= "myTa.text='got Event';" x= "208" y= "213" ></my:MyButton>
<my:MyButton id= "my2" EventTest= "handleEvent(event)" x= "209" y= "258" ></my:MyButton>
<s:TextArea id= "myTa" x= "141" y= "55" />
</s:Application>

 

 

 

解释:

1· 在MyButton中将[Event]标签放入Metadata标签中,则告诉编译器该空间会产生该种Event,

如果此时dispatchEvent出去的Event不是该种类型 则在编译期间及会报错

2· 在主类中<my:MyButton id="my1" EventTest="myTa.text='got Event';" x="208" y="213"></my:MyButton>

由于EventTest已经在Metadata做了标记,即可使用代码提示功能找到相应的Event

EventTest="myTa.text='got Event';" 表示为EventTest 这个Event注册一个监听者,监听到信息后处理方式为

myTa.text='got Event' , same as the EventTest="handleEvent(event)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值