论坛首页 编程语言技术论坛

Flex Event标签

浏览 6432 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-09-06  

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)


 

   发表时间:2009-11-27  
弱弱问下,代码片段是用什么工具生成的?很不错
0 请登录后投票
   发表时间:2009-12-01  
http://fayaa.com/code/new/
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics