[转]Flex组件的生命周期,初始化过程中的一些顺序
写flex组件,了解UIComponent的生命周期(life cycle)很重要,尤其是初始化(initialization)的过程很复杂,贴个文件可以很容易看清这个顺序
文件如下:
package
{
import flash.events.Event;
import mx.core.UIComponent;
import mx.events.FlexEvent;
public class UILifeCycle extends UIComponent
{
public function UILifeCycle()
{
super();
trace("***[ constructor ]");
//flash.events.Event
this.addEventListener(Event.ADDED,onAdded);
this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
this.addEventListener(Event.REMOVED,onRemoved);
this.addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage);
//mx.events.FlexEvent;
this.addEventListener(FlexEvent.ADD,onAdd);
this.addEventListener(FlexEvent.REMOVE,onRemove);
this.addEventListener(FlexEvent.PREINITIALIZE,onPreinitialize);
this.addEventListener(FlexEvent.INITIALIZE,onInitialize);
this.addEventListener(FlexEvent.CREATION_COMPLETE,onCreationComplete);
}
//flash.events.Event
private function onAdded(eo:Event):void{trace(eo.toString())};
private function onAddedToStage(eo:Event):void{trace(eo.toString())};
private function onRemoved(eo:Event):void {trace(eo.toString())};
private function onRemovedFromStage(eo:Event):void {trace(eo.toString())};
//mx.events.FlexEvent;
private function onAdd(eo:FlexEvent):void {trace(eo.toString())};
private function onRemove(eo:FlexEvent):void{trace(eo.toString())};
private function onPreinitialize(eo:FlexEvent):void{trace(eo.toString())};
private function onInitialize(eo:FlexEvent):void{trace(eo.toString())};
private function onCreationComplete(eo:FlexEvent):void{trace(eo.toString())};
override protected function createChildren():void{trace("***[ createChildren ]")}
override protected function commitProperties():void{trace("***[ commitProperties ]")}
override protected function measure():void{trace("***[ measure ]")}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{trace("***[ updateDisplayList]")}
}
}
拖到舞台上发布就可以看到trace信息了
***[ constructor ]
[Event type="added" bubbles=true cancelable=false eventPhase=2]
[Event type="add" bubbles=false cancelable=false eventPhase=2]
[Event type="preinitialize" bubbles=false cancelable=false eventPhase=2]
***[ createChildren ]
[Event type="initialize" bubbles=false cancelable=false eventPhase=2]
***[ commitProperties ]
***[ measure ]
***[ updateDisplayList]
[Event type="creationComplete" bubbles=false cancelable=false eventPhase=2]
[Event type="addedToStage" bubbles=false cancelable=false eventPhase=2]
为了便于理解Flex里面组件的事件是如何发生的,我写了这么一些代码:
mxml:<mx:Something ...... addedToStage="trace('addedToStage')"
creationComplete="trace('creationComplete')"
enterFrame="if (!enterframeflag) {trace('enterFrame'); enterframeflag=true;}"
initialize="trace('initialize')"
preinitialize="trace('preinitialize')"
resize="trace('resize')"
show="trace('show')"/>然后再debug模式下运行,输出结果如下:
preinitialize
initialize
enterFrame
resize
[SWF] C:\Documents and Settings\polo\Desktop\study\testmenu\bin-debug\testmenu.swf - 877,825 bytes after decompression
creationComplete
addedToStage再运行一次:
preinitialize
initialize
[SWF] C:\Documents and Settings\polo\Desktop\study\testmenu\bin-debug\testmenu.swf - 877,825 bytes after decompression
enterFrame
resize
creationComplete
addedToStage[SWF]一行是解压过程,可以忽略不计。
剩下的内容就是组件事件的发生顺序了。
预初始化->初始化->进入桢->改变大小->创建结束->添加到场景。
而show事件没有发生。