转 flex metadata tag学习

Flex引入了元数据标签的概念,它告诉编译器如何编译这段Flex 或actionscript代码。大多数人都使用过[Bindable]标签,元数据标签是一种 特殊的标签,它在代码中的作用就是向编译器提供如何编译程序的信息。实际上,这些标签并没有被编译到生成的SWF文件中,而只是 告诉编译器如何生成SWF 文件。 这篇Post结合网上相关的 资料 以及Adobe的 help resource center的相关材料 介绍了16个元数据标签。

[ArrayElementType]

实际上,定义一个数组通常来说是一件很平常的事情,因为数组中的元素可以是任何类型的。不过,使用ArrayElementType元数据标签可以让你定义数组元素的数据类型。下面的例子展示了如何使用[ArrayElementType]:

[ArrayElementType(”String”)]
public var arrayOfStrings:Array;

[ArrayElementType(”Number”)]
public var arrayOfNumbers:Array;

[ArrayElementType(”mx.core.UIComponent”)]
public var arrayOfUIComponents:Array;

[Bindable]

Bindable元数据标签是最经常用到的一个元数据标签,因为它使程序组件之间的数据同步变得很容易。Bindable可以用来绑定简单数据类型、类、复杂数据类型以及函数。绑定数据的时候,你必须先使用元数据标签定义一下数据。Bindable也可以用来绑定到事件。

[DefaultProperty]

DefaultProperty元数据标签用来将一个单一属性设定为某个类的默认属性。它允许在一个容器标签内设定属性,而不用定义属性的名字。一个简单的例子就是一个自定义Button类。 Listing 3 展示了一个简单的Button类,它将label属性设定为了DefaultProperty。 Listing 4 展示了label属性是如何在自定义Button标签中作为一个字符串定义的。

[Deprecated]

A class or class elements marked as deprecated is one which is considered obsolete, and whose use is discouraged in the current release. While the class or class element still works, its use can generate compiler warnings.

[Embed]

Embed元数据标签用来导入图片到程序。可以通过两种方式使用Embed。你可以将图片嵌入到ActionScript中并将其指派给一个变量(如同下面代码中的第一个例子),或者你也可以将图片直接指派给组件的属性(使用下面代码中的第二个例子所示的语法规则)。

例1:

[Embed(source=”myIcon.gif”)]
[Bindable]
public var myIcon:Class;<mx:Button label=”Icon Button 1″ icon=”{myIcon}”/>
<mx:Button label=”Icon Button 2″ icon=”{myIcon}”/>

例2:

<mx:Button label=”Icon Button 1″ icon=”@Embed(source=myIcon.gif’)”/><mx:Button label=”Icon Button 2″ icon=”@Embed(source=myIcon.gif’)”/>

上面这两个例子产生的结果是一样的。创建myIcon类的好处是,它在一个类中只定义一次并可以绑定到程序中的多个组件。

[Event]

Event元数据标签用来声明那些被自定义类分派的事件。将这个元数据标签添加到类定义中之后,你就可以在MXML标签中添加事件处理函数来初始化该自定义类。

[Effect]

Effect元数据标签用来定义一个自定义效果,当某个事件发生的时候该效果会被分派。

[IconFile]

IconFile 是用来定义一个jpg,gif或者png文件的文件名的,它在你的自定义类中作为图标来使用。[Embed]元数据标签可以用来嵌入图片、SWF文件、音 乐文件以及视频文件等,而IconFile则只是用来嵌入用来作为自定义类图标的文件。下面是一个IconFile的例子:

[IconFile(”icon.png”)]
public class CustomButton extends Button
{}

[Inspectable]

在使用Flex Builder 2的时候,你可能会希望某些自定义组件的属性在代码提示和属性检查器(property inspector)中显示,Inspectable元数据标签就是用来定义那些属性的。

[InstanceType]

当在一个模板对象中声明一个像IDeferredInstance这样的变量时,InstanceType元数据标签就用来声明对象的类型。下面是InstanceType的用法:

[InstanceType(”package.className”)]

[NonCommittingChangeEvent]

NonCommittingChangeEvent元数据标签在某个特定事件发生的时候可以防止变量在事件发生的过程中被更改。

[RemoteClass]

RemoteClass 可以用来将一个ActionScript类绑定到一个Java类或一个ColdFusion CFC。这样做可以自动转换数据类型。下面的例子将包com.mydomain中的名为MyClass的ActionScript类绑定到了同一个包中名 为MyClass的Java类:

package com.mydomain {
[Bindable]
[RemoteClass(alias=”com.mydomain.MyClass”)]
public class MyClass {
public var id:int; public var myText:String;

}
}

[Style]

Style元数据标签用来为组件定义自定义样式属性的。只需要简单地将Sytle元数据标签添加到类的定义当然,然后就可以使用getSytle方法获取它的值了。

标签描述
[ArrayElementType]在数组中定义所有允许的数据类型。
[Bindable]声明一个属性可以在代码中使用数据绑定表达式。
[DefaultProperty]定义组件在MXML中使用时的默认属性的名称。
[Deprecated]标识一个类或类的元素是不推荐使用的,这样编译器能认出他并在被调用时给出一个警告信息。
[Effect]定义MXML的特效属性名称。
[Embed]在编译时导入 JPED,PNG,SVG 或 SWF文件。也会导入SWC文件的图片资源。他的功能等同于MXML中的 @Embe 语法。
[Event]定义MXML的事件属性的名称和类型。
[Exclude]使 Flex Builder 的标签检查器忽略类元素。语法类似 [Exclude(name=”label”, kind=”property”)]
[ExcludeClass]是 Flex Builder 标签检查器忽略类。这相当于ASDoc中的 @private 标签。
[IconFile]定义一个图标文件,使组件在Adobe Flex Builder的插入面板中以该图标显示。
[Inspectable]定义一个属性的列表,供组件使用者在 Flex Builder 的的属性提示和标签检查中使用。同时也定义了属性的可用值。
[InstanceType]指定了 IDeferredInstance 中的,属性接受的数据类型。
[NonCommittingChangeEvent]定义一个事件过渡触发器。
[RemoteClass]把一个ActionScript对象映射到Java对象。
[Style]为组件的样式属性定义一个MXML标签属性。
[Transient]当一个 ActionScript 对象被映射到Java对象后,被该标签标识的属性,会从发送到服务端的数据中忽略掉。

thanks :    http://www.geoinformatics.cn/?p=899

English help document:

About metadata tags

Metadata tags provide information to the Flex compiler that describes how your components are used in a Flex application. For example, you might create a component that defines a new event. To make that event known to the Flex compiler so that you can reference it in MXML, you insert the [Event] metadata tag into your component, as the following ActionScript class definition shows
flex代码
  1. [Event(name="enableChanged", type="flash.events.Event")]  
  2. class ModalText extends TextArea {  
  3.     ...  
  4. }  
In this example, the [Event] metadata tag specifies the event name and the class that defines the type of the event object dispatched by the event. After you identify the event to the Flex compiler, you can reference it in MXML, as the following example shows:
flex代码
  1. // Add the [Event] metadata tag outside of the class file.   
  2. [Event(name="enableChange", type="flash.events.Event")]   
  3. public class ModalText extends TextArea {  
  4.   
  5.     ...  
  6.   
  7.     // Define class properties/methods  
  8.     private var _enableTA:Boolean;  
  9.   
  10.     // Add the [Inspectable] metadata tag before the individual property.   
  11.     [Inspectable(defaultValue="false")]   
  12.     public function set enableTA(val:Boolean):void {  
  13.         _enableTA = val;  
  14.         this.enabled = val;  
  15.       
  16.         // Define event object, initialize it, then dispatch it.   
  17.         var eventObj:Event = new Event("enableChange");  
  18.         dispatchEvent(eventObj);  
  19.     }  
  20. }  

If you omit the [Event] metadata tag from your class definition, Flex issues a syntax error when it compiles your MXML file. The error message indicates that Flex does not recognize the enableChanged property.
The Flex compiler recognizes component metadata statements in your ActionScript class files and MXML files. The metadata tags define component attributes, data binding properties, events, and other properties of the component. Flex interprets these statements during compilation; they are never interpreted during run time. 
Metadata statements are associated with a class declaration, an individual data field, or a method. They are bound to the next line in the file. When you define a component property or method, add the metadata tag on the line before the property or method declaration. 
Metadata tags in ActionScript
In an ActionScript file, when you define component events or other aspects of a component that affect more than a single property, you add the metadata tag outside the class definition so that the metadata is bound to the entire class, as the following example shows: 

flex代码:

  1. // Add the [Event] metadata tag outside of the class file.   
  2. [Event(name="enableChange", type="flash.events.Event")]   
  3. public class ModalText extends TextArea {  
  4.   
  5.     ...  
  6.   
  7.     // Define class properties/methods  
  8.     private var _enableTA:Boolean;  
  9.   
  10.     // Add the [Inspectable] metadata tag before the individual property.   
  11.     [Inspectable(defaultValue="false")]   
  12.     public function set enableTA(val:Boolean):void {  
  13.         _enableTA = val;  
  14.         this.enabled = val;  
  15.       
  16.         // Define event object, initialize it, then dispatch it.   
  17.         var eventObj:Event = new Event("enableChange");  
  18.         dispatchEvent(eventObj);  
  19.     }  
  20. }  

In this example, you add the [Event] metadata tag before the class definition to indicate that the class dispatches an event named enableChanged. You also include the [Inspectable] metadata tag to indicate the default value of the property for Adobe® Flex™ Builder™. For more information on using this tag, see Inspectable metadata tag.
Metatdata tags in MXML
In an MXML file, you insert the metadata tags either in an <mx:Script> block along with your ActionScript code, or in an <mx:Metadata> block, as the following example shows:
flex代码
  1. <?xml version="1.0"?>  
  2. <!-- TextAreaEnabled.mxml -->  
  3. <mx:TextArea xmlns:mx="http://www.adobe.com/2006/mxml">  
  4.   
  5.     <mx:Metadata>   
  6.         [Event(name="enableChange", type="flash.events.Event")]   
  7.     </mx:Metadata>   
  8.   
  9.     <mx:Script>  
  10.         <!--[CDATA[  
  11.   
  12.             // Import Event class.  
  13.             import flash.events.Event;  
  14.   
  15.             // Define class properties and methods.  
  16.             private var _enableTA:Boolean;  
  17.   
  18.             // Add the [Inspectable] metadata tag before the individual property.   
  19.             [Inspectable(defaultValue="false")]   
  20.             public function set enableTA(val:Boolean):void {  
  21.                 _enableTA = val;  
  22.                 this.enabled = val;  
  23.       
  24.                 // Define event object, initialize it, then dispatch it.   
  25.                 var eventObj:Event = new Event("enableChange");  
  26.                 dispatchEvent(eventObj);  
  27.             }  
  28.         ]]-->  
  29.     </mx:Script>  
  30. </mx:TextArea>  
A key difference between the <mx:Metadata> and <mx:Script> tags is that text within the <mx:Metadata> tag is inserted before the generated class declaration, but text within <mx:Script> tag is inserted in the body of the generated class declaration. Therefore, metadata tags like [Event] and [Effect] must go in an <mx:Metadata> tag, but the [Bindable] and [Embed] metadata tags must go in an <mx:Script> tag.
flex代码
  1. Listing 1 A simple use of [Bindable]  
  2.   
  3. <?xml version="1.0" encoding="utf-8"?>  
  4. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
  5. backgroundColor="#FFFFFF">  
  6. <mx:Script>  
  7. <!--[CDATA[  
  8. [Bindable]  
  9. private var me:String="Rich Tretola";  
  10. ]]>  
  11. </mx:Script>  
  12. <mx:Panel title="Simple Binding" width="500" height="90"  
  13. paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom=" 10" layout="horizontal">  
  14. <mx:Label text="{me}"/>  
  15. </mx:Panel>  
  16. </mx:Application>  
  17.   
  18. Listing 2 Using [Bindable] with getters and setters  
  19.   
  20. <?xml version="1.0" encoding="utf-8"?>  
  21. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">  
  22. <mx:Script>  
  23. <![CDATA[  
  24. private var _phoneNumber:String = �";  
  25. // Bind getter function to phoneNumberChanged event  
  26. [Bindable(event="phoneNumberChanged")]  
  27. public function get phoneNumber():String {  
  28. return _phoneNumber;  
  29. }  
  30. // Setter method.  
  31. public function set phoneNumber(value:String):void {  
  32. if (value.length<10) {  
  33. _phoneNumber = value;  
  34. else {  
  35. _phoneNumber = phoneFormatter.format(value);  
  36. }  
  37. // Create and dispatch event  
  38. var eventObj:Event = new Event(�phoneNumberChanged");  
  39. dispatchEvent(eventObj);  
  40. }  
  41. ]]>  
  42. </mx:Script>  
  43. <mx:PhoneFormatter id="phoneFormatter"  
  44. formatString="(###) ###-####" validPatternChars="#-()  
  45. �/>  
  46. <mx:Panel title="Bind with Getters and Setters"  
  47. width="500" height="90"  
  48. paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom=" 10" layout="horizontal">  
  49. <mx:TextInput id="ti1" change="phoneNumber=ti1.text"  
  50. maxChars="10" restrict="0-9"/>  
  51. <mx:TextInput id="ti2" text="{phoneNumber}"/>  
  52. </mx:Panel>  
  53. </mx:Application>  
  54.   
  55. Listing 3 Custom Button class named MyButton  
  56.   
  57. package  
  58. {  
  59. import mx.controls.Button;  
  60. [DefaultProperty(�label")]  
  61. public class MyButton extends Button  
  62. {  
  63. }  
  64. }  
  65.   
  66. Listing 4 Using the MyButton class wih [DefaultProperty]  
  67.   
  68. <?xml version="1.0" encoding="utf-8"?>  
  69. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
  70. xmlns:comps="*">  
  71. <comps:MyButton>  
  72. <mx:String>Test</mx:String>  
  73. </comps:MyButton>  
  74. </mx:Application>  
  75.   
  76. Listing 5 Custom ButtonLabel class using [Event]  
  77.   
  78. package  
  79. {  
  80. import mx.controls.Button;  
  81. import flash.events.Event;  
  82. // Define the custom event  
  83. [Event(name="labelChanged", type="flash.events.Event")]  
  84. public class ButtonLabel extends Button {  
  85. // property to hold label value  
  86. private var _myLabel:String;  
  87. // public setter method  
  88. public function set myLabel(s:String):void {  
  89. _myLabel = s;  
  90. this.label = s;  
  91. // Create and dispatch custom event  
  92. var eventObj:Event = new Event(�labelChanged");  
  93. dispatchEvent(eventObj);  
  94. }  
  95. }  
  96. }  
  97.   
  98. Listing 6 Using the ButtonLabel class with the labelChanged [Event]  
  99.   
  100. <?xml version="1.0" encoding="utf-8"?>  
  101. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
  102. xmlns:comps="*"  
  103. backgroundColor="#FFFFFF">  
  104. <mx:Script>  
  105. <![CDATA[  
  106. import mx.controls.Alert;  
  107. import flash.events.Event;  
  108. // method to handle custom event  
  109. public function labelChanged(eventObj:Event):void {  
  110. myTA.text= myTA.text + �/n"+ eventObj.target.label;  
  111. myTA.verticalScrollPosition = myTA.verticalScrollPosition +  
  112. 20;  
  113. }  
  114. ]]>  
  115. </mx:Script>  
  116. <mx:Panel title="Event Sample" width="500" height="275"  
  117. paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom=" 10" layout="absolute">  
  118. <mx:TextInput id="buttonLabelTI"  
  119. change="myButton.myLabel=buttonLabelTI.text" x="10" y="9"/>  
  120. <!--Instantiate custom class and define method to handle label-  
  121. Changed event-->  
  122. <comps:ButtonLabel id="myButton" labelChanged="labelChanged(event)  
  123. ;"  
  124. x="10" y="39"/>  
  125. <mx:TextArea id="myTA" width="200" height="200" x="249" y="10"/>  
  126. </mx:Panel>  
  127. </mx:Application>  
  128.   
  129. Listing 7 Add the Effect metadata tag  
  130.   
  131. ...  
  132. // Define the custom event  
  133. [Event(name="labelChanged", type="flash.events.Event")]  
  134. [Effect(name="labelChangedEffect"event="labelChanged")]  
  135. public class ButtonLabel extends Button {  
  136. ...  
  137.   
  138. Listing 8 Add labelChangedEffect to the Component  
  139.   
  140. Instantiation MXML Tag  
  141. <comps:ButtonLabel id="myButton" labelChanged="labelChanged(event);"  
  142. labelChangedEffect="myEffect" x="10" y="39"/>  
  143.   
  144. Listing 9 Custom component with [Inspectable] defined  
  145.   
  146. <?xml version="1.0" encoding="utf-8"?>  
  147. <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">  
  148. <mx:Script>  
  149. <![CDATA[  
  150. [Inspectable(defaultValue="Visa",  
  151. enumeration="Visa,Mastercard,Discover,American Express"  
  152. category="Credit Card" type="String")]  
  153. public var ccType:String;  
  154. ]]>  
  155. </mx:Script>  
  156. </mx:HBox>  
  157.   
  158. Listing 10 Using [NonCommittingChangeEvent]  
  159.   
  160. <?xml version="1.0" encoding="utf-8"?>  
  161. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
  162. backgroundColor="#FFFFFF">  
  163. <mx:Script>  
  164. <![CDATA[  
  165. import flash.events.Event;  
  166. private var eventObj:Event;  
  167. [Bindable(event="triggerBinding")]  
  168. [NonCommittingChangeEvent(�change")]  
  169. private var s:String;  
  170. private function triggerBinding():void{  
  171. eventObj = new Event(�triggerBinding");  
  172. dispatchEvent(eventObj);  
  173. }  
  174. ]]-->  
  175. </mx:Script>  
  176. <mx:Panel title="NonCommittingChangeEvent Sample" width="500"  
  177. height="90"  
  178. paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom=" 10" layout="horizontal">  
  179. <mx:TextInput id="ti1" change="s=ti1.text" enter="triggerBinding()"  
  180. />  
  181. <mx:TextInput id="ti2" text="{s}" />  
  182. </mx:Panel>  
  183. </mx:Application>  
  184.   
  185. Listing 11 Custom Class CustomCircle using [Style] tags  
  186.   
  187. package  
  188. {  
  189. import mx.core.UIComponent;  
  190. [Style(name="borderColor",type="uint",format="Color",inherit="no")]  
  191. [Style(name="fillColor",type="uint",format="Color",inherit="no")]  
  192. public class CustomCircle extends UIComponent {  
  193. public function CustomCircle(){  
  194. super();  
  195. }  
  196. override protected function updateDisplayList(unscaledWidth:Number,  
  197. unscaledHeight:Number):void {  
  198. super.updateDisplayList(unscaledWidth, unscaledHeight);  
  199. graphics.lineStyle(1, getStyle(�borderColor"), 1.0);  
  200. graphics.beginFill(getStyle(�fillColor"),1.0);  
  201. graphics.drawEllipse(0,0,100,100);  
  202. }  
  203. }  
  204. }  
  205.   
  206. Listing 12 Using CustomCircle and assigning custom style properties  
  207.   
  208. <?xml version="1.0" encoding="utf-8"?>  
  209. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
  210. xmlns:comps="*"  
  211. backgroundColor="#FFFFFF">  
  212. <mx:Panel title="Style Sample" width="200" height="200"  
  213. paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom=" 10" layout="horizontal">  
  214. <comps:CustomCircle borderColor="#000000" fillColor="#FF0000" />  
  215. </mx:Panel>  
  216. </mx:Application>  

转载于:https://www.cnblogs.com/binbinjava/archive/2011/07/26/2116932.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值