Flex 初始化时的事件测试,这样初始化顺序就很明显,细致了。

flex组件在建立的时候都会经历四个事件:preinitialize, initialize, creationComplete和updateComplete(updateComplete事件在任何改动视觉的情况下都会发生,不是初始化时独有的)。我这里有个例子来检测在嵌套和平行的情况下,测试各个组件的事件抛出顺序。

 

<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" preinitialize="showEvent(event)" initialize="showEvent(event)" creationComplete="showEvent(event)" updateComplete="showEvent(event)"> <mx:Script> <!--[CDATA[ import flash.utils.getTimer; private function showEvent(event:Event):void { trace(flash.utils.getTimer().toString()+" >> " +event.currentTarget.name+" "+event.type); } ]]--> </mx:Script> <mx:Canvas id="canv1" preinitialize="showEvent(event)" initialize="showEvent(event)" creationComplete="showEvent(event)" updateComplete="showEvent(event)"> <mx:Button id="btn1" preinitialize="showEvent(event)" initialize="showEvent(event)" creationComplete="showEvent(event)" updateComplete="showEvent(event)"/> </mx:Canvas> <mx:Button id="btn2" preinitialize="showEvent(event)" initialize="showEvent(event)" creationComplete="showEvent(event)" updateComplete="showEvent(event)"/> </mx:Application>

 

输出:

349 >> InitExample0 preinitialize 356 >> canv1 preinitialize 359 >> btn1 preinitialize 369 >> btn1 initialize 370 >> canv1 initialize 371 >> btn2 preinitialize 372 >> btn2 initialize 372 >> InitExample0 initialize 476 >> btn1 creationComplete 477 >> btn1 updateComplete 477 >> canv1 creationComplete 477 >> canv1 updateComplete 477 >> btn2 creationComplete 477 >> btn2 updateComplete 478 >> InitExample0 creationComplete 481 >> InitExample0 updateComplete

 

结论:

  • 就单个组件而言,事件的抛出顺序是preinitialize,initialize,creationComplete
  • 嵌套关系的两个组件(比如Canvas和button1),preinitialize先外后内,initialize和creationComplete先内后外。只是creationComplete要在下一次update才会发生。
  • 平行关系的两个组件(比如Canvas和button2),按mxml内的顺序,只有前面组件initialize结束后,后面的组件才会抛出preinitialize。
  • 你可能感兴趣的:(function,Flex,测试,application,button,encoding)