<?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" xmlns:hz="com.hz.*" creationComplete="initHandler(event)"><fx:Script><![CDATA[import Event.AddStuEvent;import mx.collections.ArrayCollection;import mx.controls.Alert;import mx.events.FlexEvent;protected function initHandler(event:FlexEvent):void{stuInfo.stuColl=new ArrayCollection();addEventListener(AddStuEvent.ADD_USER,addBtn);}protected function addBtn(event:AddStuEvent):void{if(stuMana.stuName.text!=""&&stuMana.stuAge.text!=""){stuInfo.stuColl.addItem(event.object);}else{Alert.show("兩項都要填三!","溫馨提示");}}]]></fx:Script><fx:Declarations><!-- 將非可視元素(例如服務、值對象)放在此處 --></fx:Declarations><s:layout><s:HorizontalLayout gap="30" paddingBottom="30" paddingLeft="30" paddingRight="30" paddingTop="30"/></s:layout><hz:StuInfo id="stuInfo" width="50%" height="100%"/><hz:StuManager id="stuMana" width="50%" height="100%"/></s:Application>
上面是主application檔案:<?xml version="1.0" encoding="utf-8"?><s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" title="新增學生" creationComplete="initHandler(event)"><fx:Script><![CDATA[import Event.AddStuEvent;import mx.events.FlexEvent;public function initHandler(event:FlexEvent):void{addBtn.addEventListener(MouseEvent.CLICK,addBtnHandler);}protected function addBtnHandler(event:MouseEvent):void{var object:Object=new Object();object={sName:stuName.text,sAge:stuAge.text};dispatchEvent(new AddStuEvent(AddStuEvent.ADD_USER,object))}]]></fx:Script><fx:Declarations><!-- 將非可視元素(例如服務、值對象)放在此處 --></fx:Declarations><s:Form width="100%" height="100%"><s:FormItem label="學生姓名:" required="true" width="100%"><s:TextInput id="stuName" width="100%"/></s:FormItem><s:FormItem label="學生年齡" required="true" width="100%"><s:TextInput id="stuAge" width="100%"/></s:FormItem></s:Form><s:controlBarContent><s:Button label="新增學生:" id="addBtn"/><s:Button label="修改資訊" id="updateBtn"/></s:controlBarContent><s:controlBarLayout><s:HorizontalLayout horizontalAlign="center" gap="50" paddingTop="9" paddingBottom="9"/></s:controlBarLayout></s:Panel>
下面是自訂事件:定義了一個常量和要傳遞的參數,event屬性只設定了string,bubble預設為false。
package Event{import flash.events.Event;public class AddStuEvent extends Event{public static const ADD_USER:String="addUser"; private var _object:Object=null;public function AddStuEvent(type:String, object:Object){super(type,true);_object=object;}public function get object():Object{return _object;}public function set object(value:Object):void{_object = value;}override public function clone():Event{return new AddStuEvent(type,object);}}}
<?xml version="1.0" encoding="utf-8"?><s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" title="學生列表" creationComplete="initHandler(event)"><fx:Script><![CDATA[import mx.collections.ArrayCollection;import mx.events.FlexEvent;[Bindable]public var stuColl:ArrayCollection=null;protected function initHandler(event:FlexEvent):void{// TODO Auto-generated method stub}private function stuArr(item:Object):String{var str:String=item.sName+"哈哈"+item.sAge;return str;}]]></fx:Script><fx:Declarations><!-- 將非可視元素(例如服務、值對象)放在此處 --></fx:Declarations><s:List id="stuList" width="100%" height="100%" dataProvider="{stuColl}" labelFunction="stuArr"></s:List><s:controlBarContent><s:Button label="刪除學生" id="deleteBtn"/></s:controlBarContent></s:Panel>