后來研究發現,不是取不到,而是在createChildren的時候,自定義的objcet還沒有被賦值,只有當該組件的init事件之后才會被賦值,代碼如下:
APP:
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" xmlns:com="com.*">
<mx:Script>
<!--[CDATA[
[Bindable]
private var o:Object = {};
]]-->
</mx:Script>
<com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
<com:CustomButton label="Button"
customString="customString test" customObject="{o}"/>
</com:CustomPanel>
</mx:Application>
CustomPanel:
復制代碼 代碼如下:
package com
{
import mx.containers.Panel;
import mx.events.FlexEvent;
public class CustomPanel extends Panel
{
public function CustomPanel()
{
super();
this.addEventListener(FlexEvent.PREINITIALIZE, onPreInit);
this.addEventListener(FlexEvent.INITIALIZE, onInit);
this.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
this.addEventListener(FlexEvent.APPLICATION_COMPLETE, onAppInitComplete);
}
//=================================
// event handler
//=================================
private function onPreInit(event:FlexEvent):void
{
trace("CustomPanel[ PreInit ]");
}
private function onInit(event:FlexEvent):void
{
trace("CustomPanel[ Init ]");
}
private function onCreationComplete(event:FlexEvent):void
{
trace("CustomPanel[ CreationComplete ]");
}
private function onAppInitComplete(event:FlexEvent):void
{
trace("CustomPanel[ AppInitComplete ]");
}
//=================================
// override function
//=================================
override protected function createChildren():void
{
trace("CustomPanel[ Begin to createChildren ]");
super.createChildren();
trace("CustomPanel[ The end of createChildren ]");
}
override protected function measure():void
{
trace("CustomPanel[ Begin to measure ]");
super.measure();
trace("CustomPanel[ The end of measure ]");
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
trace("CustomPanel[ Begin to updateDisplayList ]");
super.updateDisplayList(unscaledWidth, unscaledHeight);
trace("CustomPanel[ The end of updateDisplayList ]");
}
override protected function layoutChrome(unscaledWidth:Number, unscaledHeight:Number):void
{
trace("CustomPanel[ Begin to layoutChrome ]");
super.layoutChrome(unscaledWidth, unscaledHeight);
trace("CustomPanel[ The end of layoutChrome ]");
}
override protected function commitProperties():void
{
trace("CustomButton[ Begin to commitProperties ]");
super.commitProperties();
trace("CustomButton[ The end of commitProperties ]");
}
}
}
CustomButton:
復制代碼 代碼如下:
package com
{
import mx.controls.Button;
import mx.events.FlexEvent;
public class CustomButton extends Button
{
//=================================
// properties
//=================================
private var _customString:String = "";
private var _customObject:Object = null;
public function get customString():String
{
return _customString;
}
//string
public function set customString(value:String):void
{
trace("CustomButton( set customString )");
_customString = value;
}
//object
public function get customObject():Object
{
return _customObject;
}
public function set customObject(value:Object):void
{
trace("CustomButton( set customObject )");
_customObject = value;
}
//=================================
// Constructor
//=================================
public function CustomButton()
{
trace("CustomButton( Begin to Constructor )");
super();
this.addEventListener(FlexEvent.PREINITIALIZE, onPreInit);
this.addEventListener(FlexEvent.INITIALIZE, onInit);
this.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
this.addEventListener(FlexEvent.APPLICATION_COMPLETE, onAppInitComplete);
trace("CustomButton( The end of Constructor )");
}
//=================================
// event handler
//=================================
private function onPreInit(event:FlexEvent):void
{
trace("CustomButton( PreInit )");
}
private function onInit(event:FlexEvent):void
{
trace("CustomButton( Init )");
}
private function onCreationComplete(event:FlexEvent):void
{
trace("CustomButton( Creation Complete )");
}
private function onAppInitComplete(event:FlexEvent):void
{
trace("CustomButton( AppInitComplete )");
}
//=================================
// override function
//=================================
override protected function createChildren():void
{
trace("CustomButton( Begin to createChildren )");
super.createChildren();
trace("CustomButton( The end of createChildren )");
}
override protected function measure():void
{
trace("CustomButton( Begin to measure )");
super.measure();
trace("CustomButton( The end of measure )");
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
trace("CustomButton( Begin to updateDisplayList )");
super.updateDisplayList(unscaledWidth, unscaledHeight);
trace("CustomButton( The end of updateDisplayList )");
}
override protected function commitProperties():void
{
trace("CustomButton( Begin to commitProperties )");
super.commitProperties();
trace("CustomButton( The end of commitProperties )");
}
}
}
最后運行的結果是:
CustomPanel[ PreInit ]
CustomPanel[ Begin to createChildren ]
CustomButton( Begin to Constructor )
CustomButton( The end of Constructor )
CustomButton( set customString ) //基本變量(String,Number(int,uint),Boolean)在PreInit 之前就被賦值
CustomButton( PreInit )
CustomButton( Begin to createChildren )
CustomButton( The end of createChildren )
CustomButton( Init )
CustomButton( set customObject ) //自定義對象變量在 Init之后才能被賦值,所以在createChildren中取不到
CustomPanel[ Init ] //有子控件的時候,Init 事件是在createChildren中發出的
CustomPanel[ The end of createChildren ]
CustomButton( set customObject )
CustomButton[ Begin to commitProperties ]
CustomButton[ The end of commitProperties ]
CustomButton( Begin to commitProperties )
CustomButton( The end of commitProperties )
CustomButton( Begin to measure )
CustomButton( The end of measure )
CustomPanel[ Begin to measure ]
CustomPanel[ The end of measure ]
CustomPanel[ Begin to updateDisplayList ]
CustomPanel[ Begin to layoutChrome ]
CustomPanel[ The end of layoutChrome ] //證明layoutChrome是在updateDisplayList 中被調用的
CustomPanel[ The end of updateDisplayList ]
CustomButton( Begin to updateDisplayList )
CustomButton( The end of updateDisplayList )
CustomButton( Creation Complete )
CustomPanel[ CreationComplete ]
后來又發現,在MXML中設置基本變量和對象變量有一定區別,那就是對象變量必須要用大括號{}包起來,于是就想,會不會是綁定造成的,將APP改成如下,發現跟預想中的一樣,最后的輸出結果與上面的一樣:
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" xmlns:com="com.*">
<mx:Script>
<!--[CDATA[
[Bindable]
private var o:String = "String test";//將原對象換成字符串
]]-->
</mx:Script>
<com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
<com:CustomButton label="Button"
customString="customString test" customObject="{o}"/>
</com:CustomPanel>
</mx:Application>
為了進一步確定是由于綁定造成的賦值時期不一致,我又做了如下的一個試驗,不使用綁定給對象變量賦值:
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" xmlns:com="com.*">
<com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
<com:CustomButton label="Button"
customString="customString test">
<com:customObject>
<mx:ArrayCollection/>
</com:customObject>
</com:CustomButton>
</com:CustomPanel>
</mx:Application>
其結果為:
CustomPanel[ PreInit ]
CustomPanel[ Begin to createChildren ]
CustomButton( Begin to Constructor )
CustomButton( The end of Constructor )
CustomButton( set customString )
CustomButton( set customObject ) //賦值時間與基本變量相同
CustomButton( PreInit )
CustomButton( Begin to createChildren )
CustomButton( The end of createChildren )
CustomButton( Init )
CustomPanel[ Init ]
CustomPanel[ The end of createChildren ]
CustomButton[ Begin to commitProperties ]
CustomButton[ The end of commitProperties ]
CustomButton( Begin to commitProperties )
CustomButton( The end of commitProperties )
CustomButton( Begin to measure )
CustomButton( The end of measure )
CustomPanel[ Begin to measure ]
CustomPanel[ The end of measure ]
CustomPanel[ Begin to updateDisplayList ]
CustomPanel[ Begin to layoutChrome ]
CustomPanel[ The end of layoutChrome ]
CustomPanel[ The end of updateDisplayList ]
CustomButton( Begin to updateDisplayList )
CustomButton( The end of updateDisplayList )
CustomButton( Creation Complete )
CustomPanel[ CreationComplete ]
問題確定,所以以后再createChildren 中使用自定義對象變量的時候必須要注意,否則就會出現空指針之類的問題了。