CodeQrius
CodeQrius

Reputation: 429

UIComponent base class not working

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">
     <mx:Button id="btn" label="My Button" />
</local:BaseComponentClass>

The Button never shows up at runtime.

Upvotes: 1

Views: 385

Answers (3)

Stefan
Stefan

Reputation: 1355

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?

Upvotes: 1

jeremym
jeremym

Reputation: 97

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.

Upvotes: 0

David Salzer
David Salzer

Reputation: 872

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!

Upvotes: 0

Related Questions