3 Replies Latest reply on Sep 5, 2009 10:08 AM by TheFlexGuy

    Calling a component from a the main.mxml file






















      How do I call a component from a the main.mxml file?

      <mx:Button id="btnNew" label="Launch Component " click="getComponent()" />


      main.mxml (Has the button, that I would like to launch getcomponent.mxml)

      getcomponent.mxml (The file I would like to call) (This file also has the ViewStack)

      For some reasoning it's not seeing the bpVS(view stack id). I'm getting Access of undefined property bpVS error.

      <!-- Main.mxml  -->
      <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"


         private function getComponent():void{
          bpVS.selectedIndex = 1;



      <!-- GetComponent.mxml -->


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

      <mx:ViewStack  id="bpVS"
        top="69" left="180.5"
        width="100%" height="100%"
        moveEffect="Move" showEffect="Fade"

      <mx:VBox id="vbTB" top="69" verticalGap="0">
          width="180" useHandCursor="true" buttonMode="true""
          moveEffect="Move" showEffect="Fade" hideEffect="Fade" >


      Thank you

        • 1. Re: Calling a component from a the main.mxml file

          You need to have one as the Main application (starts with the <mx:Application> tag) and all others are child components in it.  It looks like you might have two Applications? I'm can't quite tell.

          1 person found this helpful
          • 2. Re: Calling a component from the main.mxml file
            rdub405 Level 1

            This was helpful because I had two applications. But it's still not seeing my viewstack id from the main application. I thought if i added this it would see the componet.




            • 3. Re: Calling a component from the main.mxml file
              TheFlexGuy Level 2


              It won't see it just by creating the namespace that contains it.  There has to actually be an instance of it in the application.  So in your case, your main application file (lets call it MyApp.mxml) would look something like this:



              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                   <components:Main id="main"/>
                   <components:GetComponent id="getComp"/>


              Then, you can reach down (mind you this isn't best practice) into those components to access their public properties.  In a script block in MyApp.mxml, you could access the VBox with getComp.vbTB.


              I don't know the ins and outs of your application, but perhaps Main is the main application, so then you would add GetComponent as a child of it instead of both Main and GetComponent being child components of the MyApp application.  Does that clear up the confusion?