ArrayCollection:
标签赋值写法:
比如有一个自定的类:Persion.as:
package vo { [Bindable] public class Persion { public var id:String; public var age:int; public var name:String; public function Persion() { toString(); } public function toString():String{ return "{id:"+id+" age:"+age+" name:"+name+"}"; } } }
<?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/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)" xmlns:vo="vo.*"> <fx:Script> <![CDATA[ import mx.events.FlexEvent; protected function application1_creationCompleteHandler(event:FlexEvent):void { trace(ac1); //Flex,Flash,Flash Media Server,Flash Lite,AIR trace(ac2); //{id:null age:1 name:1},{id:null age:2 name:2},{id:null age:3 name:3} } ]]> </fx:Script> <fx:Declarations> <s:ArrayCollection id="ac1"> <fx:String>Flex</fx:String> <fx:String>Flash</fx:String> <fx:String>Flash Media Server</fx:String> <fx:String>Flash Lite</fx:String> <fx:String>AIR</fx:String> </s:ArrayCollection> <s:ArrayCollection id="ac2"> <vo:Persion age="1" name="1"></vo:Persion> <vo:Persion age="2" name="2"></vo:Persion> <vo:Persion age="3" name="3"></vo:Persion> </s:ArrayCollection> </fx:Declarations> </s:Application>
直接赋值写法:
// 自定义Object private var items:ArrayCollection=new ArrayCollection([ {label:'Live',value1:'N',refCodeA:'LIVE'},{label:'Trading Hierarchy',refCodeA:'Trad'},{label:'Production Demo',value1:'Y',refCodeA:'PROD'},{label:'PAT Demo',refCodeA:'PAT'},{label:'Derivative Clearance Demo',refCodeA:'DCT'},{label:'Client On-Boarding Tool',refCodeA:'CONB'} ]); // String类型 public var yesOrNoList:ArrayCollection = new ArrayCollection(['Y','N']);
Array:
标签赋值写法:
<?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/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)"> <fx:Script> <![CDATA[ import mx.events.FlexEvent; protected function application1_creationCompleteHandler(event:FlexEvent):void { trace(array1);<span style="white-space:pre"> </span>// Flex,AIR trace(array2);<span style="white-space:pre"> </span>// Flex0,Flash0,Flex1,Flash1,Flex2,Flash2,Java2 } ]]> </fx:Script> <fx:Declarations> <fx:Array id="array1"> <fx:String>Flex</fx:String> <fx:String>Flash</fx:String> <fx:String>Flash Media Server</fx:String> <fx:String>Flash Lite</fx:String> <fx:String>AIR</fx:String> </fx:Array> <fx:Array id="array2" > <fx:Array> <fx:String>Flex0</fx:String> <fx:String>Flash0</fx:String> </fx:Array> <fx:Array> <fx:String>Flex1</fx:String> <fx:String>Flash1</fx:String> </fx:Array> <fx:Array> <fx:String>Flex2</fx:String> <fx:String>Flash2</fx:String> <fx:String>Java2</fx:String> </fx:Array> </fx:Array> </fx:Declarations> </s:Application>
直接赋值写法:
var myArray:Array = ["one","two","three"]; var arr:Array = new Array("red","blue","white","black","green","yellow"); private var items:Array = [ {label:'Live',refCodeA:'CONB'} ];
Object(如果自己懒得去写自定义Object):
<?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/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)" xmlns:vo="vo.*"> <fx:Script> <![CDATA[ import mx.events.FlexEvent; protected function application1_creationCompleteHandler(event:FlexEvent):void { // 先定义,后赋值的写法(注意,这里支持嵌套) var o2:Object = new Object(); o2["column1"] = "c1"; o2["column2"] = "c2"; o2["column3"] = "c3"; o2.position = {department:"Accounting",salary:50000,title:"Junior Accountant"}; // 直接赋值写法(注意,这里支持嵌套) var person:Object = { name:"John Smith",age:22,position:{department:"Accounting",title:"Junior Accountant"},id:303 }; trace(o1); trace(o2); trace(person); } ]]> </fx:Script> <fx:Declarations> <!-- 标签定义写法 /--> <fx:Object id="o1" field1="1" field2="2" field3 = "3" /> </fx:Declarations> </s:Application>