您的位置:首页 > 其它

Flex Event标签

2011-11-02 13:48 253 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: