ITPub博客

首页 > 应用开发 > IT综合 > Flex Event标签

Flex Event标签

IT综合 作者:杨幂打真三 时间:2015-08-26 21:08:36 0 删除 编辑
转:http://blog.chinaunix.net/uid-20593721-id-1918547.html
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 tag of an MXML file.

Demo:

定义MyEvent extends Event


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

<?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

<?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· 在主类中

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

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

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



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29032948/viewspace-1783494/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Flex Cairngorm详解
请登录后发表评论 登录
全部评论

注册时间:2015-06-03

  • 博文量
    28
  • 访问量
    83677