2010-01-04 45 views
1

我有一個BaseComponentClass,我用作所有我的自定義組件擴展的類。 出於某種原因,我的自定義組件在運行時不顯示。我沒有收到任何編譯或運行時錯誤。 我正在實施所有受保護的UIComponent方法。 我的代碼如下所示:UIComponent基類不工作

public class BaseComponentClass extends UIComponent 
{ 
    public function BaseComponentClass() 
    { 
     super(); 
    } 

    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void 
    { 
      super.updateDisplayList(unscaledWidth, unscaledHeight); 
    }  

    override protected function createChildren():void 
    { 
     super.createChildren(); 

     for (var i:uint=0; i < super.numChildren; i++) 
     { 
      var childObj:DisplayObject = super.getChildAt(i); 
      addChild(childObj); 
     }   
    } 

    override protected function commitProperties():void 
    { 
     super.commitProperties(); 
    } 

    override protected function measure():void 
    { 
     super.measure(); 
    } 
} 

然後,我用它作爲基類在我的MXML自定義組件有點像這樣:

<local:BaseComponentClass xmlns:local="local.com.*" xmlns:mx="http://www.adobe.com/2006/mxml"> 
    <mx:Button id="btn" label="My Button" /> 
</local:BaseComponentClass> 

的按鈕從不在運行時顯示出來。

+0

如果您不覆蓋受保護的方法(因爲您目前沒有在其中做任何額外的工作)會發生什麼? – 2010-01-05 00:04:11

+0

什麼都沒有發生。我上面描述的問題仍然存在。 – CodeQrius 2010-01-05 00:10:39

回答

1

顯然你想添加子對象到你的BaseComponent。

爲什麼不從一個支持此功能的類繼承,如Box或Canvas?

+0

我也想過這樣做,但我想知道如果這樣做會使我的Flex應用程序更重,與使用UIComponent作爲基礎相比。 – CodeQrius 2010-01-05 01:11:56

+0

爲什麼你需要基類?什麼是你的組件之間的共享代碼,不是UI組件的一部分,上面沒有顯示?也許有另一種方式。否則,使用容器類作爲基類只會給你帶有功能容器的所有好處,如自動佈局等。 – Stefan 2010-01-05 02:03:03

0

您是否嘗試設置該組件的寬度和高度?

缺省狀態下,UIComponent的寬度和高度都設置爲0

,你可以在這裏看到的默認值的文件,並相應地改變它的基本組件上: http://livedocs.adobe.com/flex/3/langref/mx/core/UIComponent.html

祝你好運!

+0

是的。我做到了。寬度和高度在Flex應用中生效,但沒有顯示按鈕。 – CodeQrius 2010-01-04 19:18:36

0

我不確定最佳實踐,所以在實現它之前先研究一下,但如果是我,我會創建一個數組屬性(子類可以從uicomponent中獲得),然後在類的某個地方使用defaultProperty元數據標籤([DefaultProperty(「children」)])。

如果您要調試代碼並在for循環中放置一個斷點,您將永遠不會碰到addChild代碼。事實上,createChildren(uiComponent.createChildren)的goto定義(f3),你會發現它是空的。您必須在您創建的默認屬性設置器中顯式調用addChild。你最好打賭,如果你總是將這個組件作爲一個容器類來使用,那就是擴展Container。 F3進入這些課程以獲得最佳實踐的感受。