views:

75

answers:

3

I have a BaseComponentClass that I am using as the class that all my custom components extend. For some reason, none of my custom components show up at runtime. I am not getting any compile or runtime errors either. I am implementing all the protected UIComponent methods. My code looks like this:

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();
    }
}

Then I use it as the Base class in my mxml custom components somewhat like this:

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

The Button never shows up at runtime.

A: 

Did you try to set a width and height to that component?

As default, UIComponent width and height are set to 0.

you can see the default values documentation here, and change it accordingly on your base component: http://livedocs.adobe.com/flex/3/langref/mx/core/UIComponent.html

Good luck!

yn2
Yes. I did. The width and height took effect on the flex app, but no button showed up.
Q-rius
A: 

I'm not sure of the best practices, so do research this before implementing it, but if it were me I would create an array property (children is available from uicomponent), then somewhere in the class use the defaultProperty metadata tag ( [DefaultProperty("children")] ).

If you were to debug your code and put a breakpoint in the for loop, you would never hit the addChild code. infact, goto definition (f3) of createChildren (uiComponent.createChildren) and you will find it empty. You have to explicitly call addChild in the default property setter that you create. Your better bet, if you're always going to use this component as a container-like class, is to just extend Container. F3 down into those classes to get a feel for best practices.

jeremym
+1  A: 

Apparently you want to add child objects to your BaseComponent.

Why don't you inherit from a class that supports this functionality, such as Box or Canvas?

Stefan
I thought of doing that too, but I was wondering if doing that would make my flex app heavier, as compared to using UIComponent as the base.
Q-rius
Why do you need the base class at all? What's the shared code amongst your components that is not part of a UI Component and that is not shown above? Maybe there is another way. Otherwise, using the container classes as base class just gives you all the benefits of already functional containers with features like auto layout etc. .
Stefan