云自無心水自閑

          天平山上白云泉,云自無心水自閑。何必奔沖山下去,更添波浪向人間!
          posts - 288, comments - 524, trackbacks - 0, articles - 6
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Flex的事件流

          Posted on 2007-02-11 23:50 云自無心水自閑 閱讀(3929) 評論(1)  編輯  收藏 所屬分類: FlexFlex2

          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!

          ?




          評論

          # re: Flex的事件流  回復  更多評論   

          2007-08-16 23:35 by Tomato
          Thanks, helped me a lot ~ :-)
          主站蜘蛛池模板: 莫力| 梁平县| 富顺县| 安宁市| 城固县| 六枝特区| 漳平市| 克东县| 壶关县| 岢岚县| 泌阳县| 辽阳县| 确山县| 渝中区| 辽宁省| 霸州市| 凌海市| 石城县| 康保县| 台中县| 富蕴县| 横山县| 花垣县| 明光市| 陵川县| 龙山县| 靖江市| 巴楚县| 株洲县| 高阳县| 繁昌县| 会同县| 安平县| 台安县| 大冶市| 重庆市| 平顺县| 长治县| 杨浦区| 阿巴嘎旗| 淮安市|