關(guān)于Flex 初始化的research
更新時(shí)間:2009年09月28日 18:01:46 作者:
前些天在寫(xiě)一個(gè)自定義的UI組件的時(shí)候,發(fā)現(xiàn)在override createChildren的,只能取到基本類(lèi)型的自定義變量,而取不到Object類(lèi)型的自定義變量。
后來(lái)研究發(fā)現(xiàn),不是取不到,而是在createChildren的時(shí)候,自定義的objcet還沒(méi)有被賦值,只有當(dāng)該組件的init事件之后才會(huì)被賦值,代碼如下:
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 )");
}
}
}
最后運(yùn)行的結(jié)果是:
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 ) //自定義對(duì)象變量在 Init之后才能被賦值,所以在createChildren中取不到
CustomPanel[ Init ] //有子控件的時(shí)候,Init 事件是在createChildren中發(fā)出的
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 中被調(diào)用的
CustomPanel[ The end of updateDisplayList ]
CustomButton( Begin to updateDisplayList )
CustomButton( The end of updateDisplayList )
CustomButton( Creation Complete )
CustomPanel[ CreationComplete ]
后來(lái)又發(fā)現(xiàn),在MXML中設(shè)置基本變量和對(duì)象變量有一定區(qū)別,那就是對(duì)象變量必須要用大括號(hào){}包起來(lái),于是就想,會(huì)不會(huì)是綁定造成的,將APP改成如下,發(fā)現(xiàn)跟預(yù)想中的一樣,最后的輸出結(jié)果與上面的一樣:
<?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";//將原對(duì)象換成字符串
]]-->
</mx:Script>
<com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
<com:CustomButton label="Button"
customString="customString test" customObject="{o}"/>
</com:CustomPanel>
</mx:Application>
為了進(jìn)一步確定是由于綁定造成的賦值時(shí)期不一致,我又做了如下的一個(gè)試驗(yàn),不使用綁定給對(duì)象變量賦值:
<?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>
其結(jié)果為:
CustomPanel[ PreInit ]
CustomPanel[ Begin to createChildren ]
CustomButton( Begin to Constructor )
CustomButton( The end of Constructor )
CustomButton( set customString )
CustomButton( set customObject ) //賦值時(shí)間與基本變量相同
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 ]
問(wèn)題確定,所以以后再createChildren 中使用自定義對(duì)象變量的時(shí)候必須要注意,否則就會(huì)出現(xiàn)空指針之類(lèi)的問(wèn)題了。
APP:
復(fù)制代碼 代碼如下:
<?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:
復(fù)制代碼 代碼如下:
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:
復(fù)制代碼 代碼如下:
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 )");
}
}
}
最后運(yùn)行的結(jié)果是:
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 ) //自定義對(duì)象變量在 Init之后才能被賦值,所以在createChildren中取不到
CustomPanel[ Init ] //有子控件的時(shí)候,Init 事件是在createChildren中發(fā)出的
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 中被調(diào)用的
CustomPanel[ The end of updateDisplayList ]
CustomButton( Begin to updateDisplayList )
CustomButton( The end of updateDisplayList )
CustomButton( Creation Complete )
CustomPanel[ CreationComplete ]
后來(lái)又發(fā)現(xiàn),在MXML中設(shè)置基本變量和對(duì)象變量有一定區(qū)別,那就是對(duì)象變量必須要用大括號(hào){}包起來(lái),于是就想,會(huì)不會(huì)是綁定造成的,將APP改成如下,發(fā)現(xiàn)跟預(yù)想中的一樣,最后的輸出結(jié)果與上面的一樣:
復(fù)制代碼 代碼如下:
<?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";//將原對(duì)象換成字符串
]]-->
</mx:Script>
<com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
<com:CustomButton label="Button"
customString="customString test" customObject="{o}"/>
</com:CustomPanel>
</mx:Application>
為了進(jìn)一步確定是由于綁定造成的賦值時(shí)期不一致,我又做了如下的一個(gè)試驗(yàn),不使用綁定給對(duì)象變量賦值:
復(fù)制代碼 代碼如下:
<?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>
其結(jié)果為:
CustomPanel[ PreInit ]
CustomPanel[ Begin to createChildren ]
CustomButton( Begin to Constructor )
CustomButton( The end of Constructor )
CustomButton( set customString )
CustomButton( set customObject ) //賦值時(shí)間與基本變量相同
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 ]
問(wèn)題確定,所以以后再createChildren 中使用自定義對(duì)象變量的時(shí)候必須要注意,否則就會(huì)出現(xiàn)空指針之類(lèi)的問(wèn)題了。
相關(guān)文章
flex 簡(jiǎn)單例子(含實(shí)例效果圖 源碼)
要過(guò)節(jié)了,工作任務(wù)也完成了,閑來(lái)沒(méi)事,研究svg,感覺(jué)市場(chǎng)上對(duì)svg支持度不高,導(dǎo)致了這項(xiàng)技術(shù)不能夠被IT群眾充分的接納,一個(gè)沒(méi)有人支持的技術(shù),她必然也不會(huì)有很大的前景。研究來(lái)研究去,還是覺(jué)得flex比較好玩。2009-09-09
FluorineFx.NET的認(rèn)證(Authentication )與授權(quán)(Authorization)Flex與.NE
FluorineFx.NET的認(rèn)證(Authentication )與授權(quán)(Authorization)和ASP.NET中的大同小異,核實(shí)用戶(hù)的身份既為認(rèn)證,授權(quán)則是確定一個(gè)用戶(hù)是否有某種執(zhí)行權(quán)限2009-06-06
Flex與.NET互操作(十三):FluorineFx.Net實(shí)現(xiàn)視頻錄制與視頻回放
本文主要介紹使用FluorineFx.Net來(lái)實(shí)現(xiàn)視頻錄制與視頻回放,F(xiàn)luorineFx如同F(xiàn)MS一樣,除了有AMF通信,RTMP協(xié)議,RPC 和遠(yuǎn)程共享對(duì)象外,它同樣具備視頻流服務(wù)的功能。2009-06-06
Flex 如何得到itemRenderer里面的內(nèi)容
itemRenderer里面的內(nèi)容 獲取技巧。2009-07-07
讓Flex Builder 3.0與Eclipse3.4整合起來(lái)
Flex Builder 3.0 For Eclipse 3.3 安裝方法2009-02-02
Flex 處理服務(wù)器端傳來(lái)的數(shù)據(jù)
對(duì)于Java端返回的各種Java類(lèi)型的對(duì)象,F(xiàn)lex中能否有相應(yīng)的數(shù)據(jù)類(lèi)型來(lái)映射。這是Flex與服務(wù)器通信使用remoteObject的關(guān)鍵。2009-08-08

