Flex最强大的特性之一就是它在标签和ActionScript类之间创建了一个简单的映射。
就像下面的样子:
?1 —— 每个标签相当于一个以标签名为类名的类的一个实例。
?2—— 标签的每个属性(attribute)转变为对象中的一个property。
?3—— 每个标签中的id属性转变为相应实例中的一个变量。
例如? 类:
- public?class?Contact ?
- { ?
- ????public?var?home?:?Location; ?
- ????public?var?work?:?Location; ?
- ????public?var?firstname?:?String; ?
- ????public?var?lastname?:?String; ?
-
????public?var?isFriend?:?Boolean?=?false; ?
- } ?
标签:<Contact id="myContact" firstname="Susan" lastname="Smith" isfriend="true" />
相当于:
- var?myContact?:?Contact?=?new?Contact(); ?
-
myContact.firstname?=?"Susan"; ?
-
myContact.lastname="Smith"; ?
-
myContact.isFriend=true; ?
所以就可以这样:
代码:
- <?xml?version="1.0"?encoding="utf-8"?>?
-
<contr:Test05?xmlns:fx="http://ns.adobe.com/mxml/2009"? ?
-
??????????????xmlns:s="library://ns.adobe.com/flex/spark"? ?
-
??????????????xmlns:mx="library://ns.adobe.com/flex/mx"?xmlns:contr="contr.*"?width="400"?height="300">?
-
????<fx:Declarations>?
-
?????????
-
????</fx:Declarations>?
-
????<mx:VBox?width="100"?height="100">?
-
????????<s:Label?width="30"?height="30"?text="12312"?fontSize="40"/>?
-
????</mx:VBox>?
-
</contr:Test05>?
?
- package?contr ?
- { ?
- ????import?flash.events.MouseEvent; ?
- ????import?mx.containers.TitleWindow; ?
- ????import?mx.controls.Button; ?
- ????import?mx.controls.LinkButton; ?
- ????import?mx.managers.PopUpManager; ?
- ????public?class?Test05?extends?TitleWindow ?
- ????{ ?
- ????????private?var?helpButton?:?LinkButton; ?
- ????????private?var?_closeButton?:?Button; ?
- ????????public?function?Test05?() ?
- ????????{ ?
-
????????????title?=?"Custom?TitleWindow"; ?
-
????????????showCloseButton?=?true; ?
- ????????} ?
- ????????private?function?get?closeButton?()?:?Button ?
- ????????{ ?
- ????????????if?(!?_closeButton) ?
- ????????????{ ?
-
????????????????for?(var?i?:?int?=?0;?i?<?titleBar.numChildren;?++?i) ?
- ????????????????{ ?
- ????????????????????if?(titleBar.getChildAt?(i)?is?Button?&& ?
- ????????????????????????titleBar.getChildAt?(i)?!=?helpButton) ?
- ????????????????????{ ?
-
????????????????????????_closeButton?=?titleBar.getChildAt?(i)?as ?
- ????????????????????????????Button; ?
- ????????????????????} ?
- ????????????????} ?
- ????????????} ?
- ????????????return?_closeButton; ?
- ????????} ?
- ????????override?protected?function?createChildren?()?:?void ?
- ????????{ ?
- ????????????super.createChildren?();???????????????? ?
- ????????????if?(!?helpButton) ?
- ????????????{ ?
-
????????????????helpButton?=?new?LinkButton?(); ?
-
????????????????helpButton.label?=?"Help";?????????? ?
-
????????????????helpButton.focusEnabled?=?false; ?
- ????????????????helpButton.setStyle?("paddingTop",?4); ?
- ????????????????titleBar.addChild?(helpButton); ?
- ????????????????helpButton.addEventListener(MouseEvent.CLICK,helpClick) ?
-
????????????????helpButton.owner?=?this; ?
- ????????????} ?
- ????????} ?
- ????????protected?function?helpClick(event:MouseEvent):void ?
- ????????{ ?
- ????????????PopUpManager.addPopUp(this,this,true); ?
- ????????} ?
- ????????override?protected?function?layoutChrome?(w?:?Number,?
- ??????????????????????????????????????????????????h?:?Number)?:?void ?
- ????????{ ?
- ????????????super.layoutChrome?(w,?h); ?
-
????????????var?width?:?Number?= ?
-
????????????????helpButton.getExplicitOrMeasuredWidth?(); ?
-
????????????var?height?:?Number?= ?
-
????????????????helpButton.getExplicitOrMeasuredHeight?(); ?
-
????????????var?x?:?Number?=?5; ?
-
????????????var?y?:?Number?=?5; ?
- ????????????helpButton.setActualSize?(width,?height); ?
- ????????????helpButton.move?(x,?y); ?
- ????????} ?
- ????} ?
- }??
在类里面可以使用任何这个类的属性和方法来定义要使用的组件的样式和事件逻辑。
学习ING。。。。