Posted on 2007-02-11 23:50
云自無心水自閑 閱讀(3924)
評論(1) 編輯 收藏 所屬分類:
Flex 、
Flex2
Flex是一個事件驅動的編程模型, 任何事情的發生, 其背后必然存在一個事件. 而開發者第一次看到MXML時, 很難體會到一個Xml標記的應用的事件流和實例化的生命周期. 這個對于HTML和Flash的開發者尤其會感到困惑, 因為其熟悉的方式與Flex的一點也不相似. HTML的實例化是從上到下的, Flash的執行是從Frame0開始一幀幀運行的. 而Flex則又有不同.
從我們開始學習Flex時, 我們就需要了解事件流和MXML的實例化. 我非常困惑因為我實在難以理解什么樣的事件會被觸發或者事件什么時候會被觸發. 關鍵是要理解事件的基礎并親自觀察事件流的初始化.
我們來看一個簡單的MXML的應用.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
??? xmlns:mx="??? layout="absolute"
??? backgroundGradientColors="[#67cbff, #fcffff]"
??? color="#000000"
??? fontSize="12"???
??? preinitialize="report( event , 'preinitialize' )"
??? initialize="report( event , 'initialize' )"
??? creationComplete="report( event , 'creationComplete' )"
??? applicationComplete="report( event , 'applicationComplete' )"
??? >
???
??? <mx:Script>
??????? <![CDATA[???
???????????????????
??????????? [Bindable]
???????????
??????????? public var outTextData:String="";
???????????
??????????? public function report( event:Event , value:String ):void
??????????? {
??????????????? outTextData += String( flash.utils.getTimer() ) + 'ms >> '
??????????????? + event.currentTarget + '.' + value + '\n';???
??????????? }
???????????
??????? ]]>
??? </mx:Script>
???
??? <mx:TextArea
??????? id="outTextArea"
??????? text="{ outTextData }"
??????? right="10" left="10" top="50" bottom="10" alpha="0.5"
??????? wordWrap="false"
??????? initialize="report( event , 'initialize' )"
??????? creationComplete="report( event , 'creationComplete' )"
??????? />
???
??? <mx:Button
??????? y="10" height="30" left="168" width="150"
??????? id="HelloButton"
??????? label="Say Hello"
??????? initialize="report( event , 'initialize' )"
??????? creationComplete="report( event , 'creationComplete' )"
??????? rollOver="report( event , 'rollOver' )"
??????? rollOut="report( event , 'rollOut' )"
??????? click="report( event , 'click > Hello!' )"
??????? />
???????
??? <mx:Button
??????? id="GoodByeButton"
??????? label="Say Goodbye"
??????? y="10" left="10" height="30" width="150" color="#000000"
??????? initialize="report( event , 'initialize' )"
??????? creationComplete="report( event , 'creationComplete' )"
??????? click="report( event , 'click > Goodbye!' )"
??????? />
???????
??? <mx:Button
??????? id="ClearButton"
??????? label="Clear"
??????? y="10" left="326" height="30" color="#000000" right="10"???????
??????? initialize="report( event , 'initialize' )"
??????? creationComplete="report( event , 'creationComplete' )"
??????? click="outTextData='';report( event , 'click' )"
???????? />
???
</mx:Application>
這個應用運行時, 輸出了實例流程和事件流. 這校我們就能夠看到所有事件的觸發順序. 可以發現應用啟動后, 事件的順序是一定的. 下面是輸出的內容:
167ms >> EventFlow0.preinitialize
183ms >> EventFlow0.outTextArea.initialize
187ms >> EventFlow0.HelloButton.initialize
188ms >> EventFlow0.GoodByeButton.initialize
189ms >> EventFlow0.ClearButton.initialize
189ms >> EventFlow0.initialize
243ms >> EventFlow0.outTextArea.creationComplete
243ms >> EventFlow0.HelloButton.creationComplete
243ms >> EventFlow0.GoodByeButton.creationComplete
244ms >> EventFlow0.ClearButton.creationComplete
244ms >> EventFlow0.creationComplete
246ms >> EventFlow0.applicationComplete
一旦applicationComplete事件觸發后, 組件就會在鼠標事件派發后觸發自己的事件.
1807ms >> EventFlow0.HelloButton.rollOver
2596ms >> EventFlow0.HelloButton.rollOut
2954ms >> EventFlow0.HelloButton.rollOver
3170ms >> EventFlow0.HelloButton.rollOut
3543ms >> EventFlow0.HelloButton.rollOver
4052ms >> EventFlow0.HelloButton.click > Hello!
4267ms >> EventFlow0.HelloButton.click > Hello!
4474ms >> EventFlow0.HelloButton.click > Hello!
4569ms >> EventFlow0.HelloButton.rollOut
4907ms >> EventFlow0.GoodByeButton.click > Goodbye!
5130ms >> EventFlow0.GoodByeButton.click > Goodbye!
?