Current location: Brighton, UK

Extending your own MXML component inline or in another custom component

Posted on Monday, November 24, 2008 @ 18:57 CET

Earlier I was trying building a custom MXML component in Flex 3 with some basic functionality, lets call it MainComponent. The thinking was that it would handle all that stuff and then I could create "children" of it that would extend this functionality and contain their own content and extra methods.

I tried simply extending the MXML component, by setting the root node of another custom component to MainComponent but this failed with the following:

"Error: Multiple sets of visual children have been specified for this component (component definition and component instance). at mx.core::Container/initialize()"

Googling around a bit I found this post over at Tink's about extending your own MXML components. As it turns out its not possible to do this as I was thinking, but instead you create an internal Array of children to add to the main component and handle it yourself. You add stuff to this array in MXML using the property name as a tag, but as Ely Greenfield pointed out in a comment on that post, you can bypass this by using the [DefaultProperty] metadata tag. He wrote:

When you specify a default property, you are essentially telling the compiler that when someone uses your component in MXML, whatever they put inside the tag is really a value to be assigned to your defaultProperty.

So when you extend your custom component inline you can avoid those tags. However for some reason I didn't get around this when creating a child component as its own MXML file, but whatever - its one tag, I'll live.

Check out the example here (right-click for source) and you shouldcheck out Ely's comment which was very helpful.

- paulo

Comments:

1
uiscedan writes:
Great example. I had been beating my head against a wall and just kind of decided it couldn't be done.

This is perfect for creating the mxml template or pseudo wrapper for my project.

# June 02, 2009 21:36 CET

2
Paulo replies:
@uiscedan: Glad it worked for you :)

# June 03, 2009 15:12 CET

Post a comment:

You must have Flash and JavaScript enabled to post a comment.