posts - 17, comments - 4, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          [轉]Flex的事件流

          Posted on 2007-08-27 16:38 愚人 閱讀(250) 評論(0)  編輯  收藏 所屬分類: 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="http://www.adobe.com/2006/mxml"
              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!
          轉自http://www.aygfsteel.com/usherlight/archive/2007/02/11/99340.html

          主站蜘蛛池模板: 平山县| 蓬莱市| 无棣县| 合作市| 六盘水市| 平陆县| 梁山县| 兴国县| 凌云县| 吉水县| 嵊泗县| 抚宁县| 巫山县| 恩施市| 平潭县| 延寿县| 嫩江县| 武宁县| 错那县| 长垣县| 绥阳县| 永年县| 灯塔市| 韶山市| 岗巴县| 东乌珠穆沁旗| 南溪县| 乌恰县| 永靖县| 涞水县| 新宾| 密云县| 阿克苏市| 湖口县| 柳河县| 乌海市| 拉萨市| 义马市| 安丘市| 冀州市| 尉氏县|