首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

Flex中的元数据标签(Metadata tags)1

2012-11-17 
Flex中的元数据标签(Metadata tags)一?在flex中经常会用到Bindable,Embed等标签,这就是元数据标签,flex中

Flex中的元数据标签(Metadata tags)一

?在flex中经常会用到Bindable,Embed等标签,这就是元数据标签,flex中引入了元数据标签,它告诉编译器如何编译这段代码。实际上,这些标签并没有被编译到省城的swf文件中,而是告诉编译器如何生成swf文件,有关元数据的详细介绍,请参考adobe在线文档http://livedocs.adobe.com/flex/3/html/help.html?content=metadata_3.html

????? 下面我将用实例来介绍元数据。

1 ArrayElementType

?

?

package com.beyondsoft.widgets{import flash.events.Event;import mx.containers.HBox;import mx.events.FlexEvent;public class MyTypedArrayComponent extends HBox{public function MyTypedArrayComponent(){super();}[ArrayElementType("String")]     public var stringArr:Array;    [ArrayElementType("Number")]    public var numberArr:Array;}}

?

?

?? MyTypedArrayComponent组件中,我们定义了两个数组,并确定了两个数组中元素的类型。

?

?? 调用这个组件,编译器会自动检查这连个数组的数据类型是否正确。

?

?

<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="com.beyondsoft.widgets.*"><local:MyTypedArrayComponent><local:numberArr><mx:String>222</mx:String></local:numberArr><local:stringArr><mx:Number>222</mx:Number></local:stringArr></local:MyTypedArrayComponent><local:MyTypedArrayComponent numberArr="[asd,222]" stringArr="[111,222]"/></mx:Application>

?

注意:mxml编译器只在mxml中检查数组的类型是否正确,在ActionScript是不做检查的。

?

?[ArrayElementType("elementType")]中elementType为String类型,如果elementType是as的基本类型那么可以写个类名就可以了,如果是自定义的类型则需要添加上完整的包名了。

?

2 Bindable

?

这个标签应该是用的最多的一个标签了,bindable是组件之间的数据同步变得更加简单。Bindable可以绑定简单的数据类型、类、复杂的数据类型以及函数。绑定数据的时候,必须先使用元数据标签定义一下数据。例如下面的代码

[Bindable]public var name:String;

?bindable也可以绑定事件,下面的例子展示了如何利用get和set函数将一个属性绑定到一个事件上。这个例子中有一个phoneNumber变量,使用bindable将get方法绑定到一个phoneNumberChanged事件上,在set方法中分派phoneNumberChanged事件,get方法就知道phoneNumber发生变化了。当phoneNumberChanged事件

分派的时候,第二个TextInput组件会被更新,因为他的text属性绑定到了phoneNumber变量上。

<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="com.beyondsoft.widgets.*"><mx:Script><![CDATA[private var _phoneNumber:String ="";// Bind getter function to phoneNumberChanged event[Bindable(event="phoneNumberChanged")]public function get phoneNumber():String {return _phoneNumber;}// Setter method.public function set phoneNumber(value:String):void {if (value.length<10) {_phoneNumber = value;} else {_phoneNumber = phoneFormatter.format(value);}// Create and dispatch eventvar eventObj:Event = new Event("phoneNumberChanged");dispatchEvent(eventObj);}]]></mx:Script><mx:PhoneFormatter id="phoneFormatter" formatString="(###) ###-####" /><mx:Panel title="Bind with Getters and Setters"width="500" height="90"paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom=" 10" layout="horizontal"><mx:TextInput id="ti1" change="phoneNumber=ti1.text" maxChars="10" restrict="0-9"/><mx:TextInput id="ti2" text="{phoneNumber}"/></mx:Panel></mx:Application>

?

3? DefaultProperty

?

DefaultProperty元数据标签用来将一个单一属性设定为某个类的默认属性。它允许在一个容器标签内设定属性,而不用定义属性的名字。下面的例子展示了如何给一个DateField设置默认值。

package com.beyondsoft.widgets{import mx.controls.DateField; // Define the default property.    [DefaultProperty("defaultText")]public class MyDataField extends DateField{public function MyDataField(){super();} // Define a setter method to set the text property        // to the value of the default property.        public function set defaultText(value:String):void {            if (value){            text=value;            }else{            text="2008-11-11";            }        }        public function get defaultText():String {            return text;        }}}

?调用代码:

<local:MyDataField  formatString="YYYY-MM-DD"  defaultText=""/>

?本篇就写到这里,请关注flex中的元数据标签二

?

热点排行