I would expect it to work with Canvas as the top tag. If you
want to use UIComponent, make sure you’ve read the documentation on the
component lifecycle, or see Gordon’s and my Flick & Flack article in Flex
Authority magazine. A UIComponent has to implement lifecycle methods in order
to be shown correctly
From:
flexcoders@yahoogroups.com [mailto:flexcoders@yahoogroups.com] On Behalf Of devenhariyani
Sent: Thursday, November 20, 2008 7:10 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Problems adding custom MXML component to Canvas at
runtime
Hello,
MY GOAL: to add my custom MXML component to a canvas, the custom comp
has a Label and a TextArea object inside, the user can see the Label
but the TextArea is hidden, but when a user hovers the mouse over the
Label, effect will be applied to show the TextArea.
I'm having difficulty displaying a custom MXML component on a canvas
in my application. Basically, I have a very simple MXML component
which is a UIComponent with a Label and a TextArea inside. At
runtime I create an instance of this custom MXML component and then I
set the text field for the Label and I want to add this component to
the main canvas and apply some effects to it.
However, no matter what I try the MXML component does not appear on
my canvas. When I try the same code by using a plain Label instead
of my custom MXML component I am able to see the Label appear on the
canvas and I can apply effects on it as I would expect.
Instead of using a UIComponent as the wrapper class in my custom MXML
class I have also tried using a Canvas class, but that did not make a
difference.
My custom MXML component (CustomMXMLComp.mxml):
<mx:UIComponent xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Label id="lblTitle" text="DEFAULT TEXT" />
<mx:TextArea id="txaBrief" text="DEFAULT BRIEF TEXT"
/>
</mx:UIComponent>
The code to add this component to the canvas at runtime:
<Application creationComplete="loadApp()">
<mx:Script>
private function loadApp() {
var myObj:CustomMXMLComp = new CustomMXMLComp();
this.myCnv.addChild(myObj);
myObj.x = 100;
myObj.y = 100;
//HELP: NOTHING APPEARS ON THE CANVAS!!
}
</mx:Script>
<Canvas id="myCnv" height="300"
width="399">
</Canvas>
</Application>