2 Replies Latest reply on May 27, 2010 10:09 AM by david.ferraro

    Initializing Custom MXML component




      I am new to flex and the one thing I can't seem to get a handle on is using custom mxml components. The whole not being able to use a constructor to pass data through was a tough thing to adjust to so for the most part I've been writing my own AS classes for display purposes that extend layout containers like VBox and such. This is getting cumbersome and I would really like to use MXML to save some time when writing display classes. For the most part I'm trying to use custom mxml components this way: I put the labels and other controls and then want data to display as text from the calling class. However, when I try to set the text it says that the control I am trying to set hasn't been initialized yet. My workaround is something like this.


      var data1:String = "Something";


      var customComp:CustomMXMLComponent = new CustomMXMLComponent();



      customComp.someControl = data1;


      Now why wouldn't someControl on the custom component be initialized when the actual component is initialized? Is there something I can change in the mxml component so that I don't always have to call .initialize() after creating an instance of the custom component?



        • 1. Re: Initializing Custom MXML component
          UbuntuPenguin Level 4

          .  The "initialize" stuff you had was more concerned with the lifecycle of the component.  We don't want to do that , but it is something you should learn about. I think the answer to your problem is to create Bindable variables in your mxml class,  then set those parameters.




            <Button label="{buttonLabel1}" >

            <Button label="{buttonLabel2}">

            <Label label = {labelLabel}>


               [Bindable]  public var buttonLabel1:String;

               [Bindable]  public var buttonLabel2:String;

               [Bindable]  public var labelLabel:String;





          Meanwhile , in another class ...


          var customComp:CustomMXMLComponent = new CustomMXMLComponent();


          customComp.buttonLabel1 = "Ubuntu";

          customComp.buttonLabel2 = "Rocks";

          customComp.labelLabel = "My Socks";

          1 person found this helpful
          • 2. Re: Initializing Custom MXML component
            david.ferraro Level 1

            Thanks, that makes a lot of sense.