3 Replies Latest reply on Jul 8, 2010 1:57 PM by skider

    Accessing Components From the MXML in the AS

    lkenney2 Level 1

      I have an application. For the visual component of the application, I have created an mxml file:

       

      <?xml version="1.0" encoding="utf-8"?>
      <ascentify:Main xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           width="800" height="600"
              xmlns:modules="com.ascentify.components.modules.components.*"  
           xmlns:ascentify="com.ascentify.*">
                        
           <mx:TabNavigator id="practicePlayer" width="100%" height="100%" creationPolicy="all">
                <modules:Start id="startModule" label="Start" width="100%" height="100%"/>
           </mx:TabNavigator>
      </ascentify:Main>
      

       

      'Main' is an AS 3.0 class:

       

      package com.ascentify {
      
           import mx.containers.VBox;
           import mx.containers.TabNavigator;
      
           public class Main extends Application
           {
                     public static var practicePlayer:TabNavigator = new TabNavigator();
      
                     function Main() {
                          var tab1:VBox = new VBox();
                          practicePlayer.addChild(tab1);
                     }
              }
      }
      

       

      But the VBox is not added to the TabNavigator. How can I fix this?

        • 1. Re: Accessing Components From the MXML in the AS
          skider Level 1

          The problem is that the practicePlayer in the Actionscript file and the MXML file are two different things. One is a static variable on the Main class, one is a nonstatic variable on the MXML class. When you're adding tab1 to the practicePlayer, you're adding it to the static variable, which is never added to the display list.

           

          What is the objective of the Actionscript Main class? Is it to split the Actionscript code out of the MXML? If so, it would be better to include the Actionscript code with a Script tag.

           

          Is it to create a class of objects that all have a VBox inside a Tab Navigator? If that's the case, you should probably have both the Tab Navigator and the VBox be added to the display list in the parent object.

           

          Is it for some other reason? If so, what is the reason?

          • 2. Re: Accessing Components From the MXML in the AS
            lkenney2 Level 1

            Hi skider.

             

            The Main class is over 900 lines long and contains a global variable I need in main. So what I've done now is hollowed out the mainApplication mxml:

             

            <?xml version="1.0" encoding="utf-8"?>
            <ascentify:Main xmlns:fx="http://ns.adobe.com/mxml/2009" 
                              xmlns:s="library://ns.adobe.com/flex/spark" 
                              xmlns:mx="library://ns.adobe.com/flex/mx" 
                              width="800" height="600" 
                              xmlns:ascentify="com.ascentify.*">
            </ascentify:Main>
            
            

             

            And created the GUI components in the Main class:

             

            private function createGUIContainer():void
                      {
                           playerContainer = new VBox();
                           playerContainer.percentWidth = 100;
                           playerContainer.percentHeight = 100;
                           this.addChild(playerContainer);
                           
                           playerMenu = new MenuBar();
                           playerMenu.percentWidth = 100;
                           playerContainer.addChild(playerMenu);
                           
                           playerNavigation = new UnitPlayer();
                           playerNavigation.percentWidth = 100;
                           playerNavigation.percentHeight = 100;
                           playerNavigation.creationPolicy = "all";
                           playerContainer.addChild(playerNavigation);
                           
                           startModule = new Start();
                           startModule.percentWidth = 100;
                           startModule.percentHeight = 100;
                           startModule.label = "Start";
                           playerNavigation.addChild(startModule);
                           
                           PlayerState.START_MODULE_STATE = playerNavigation.getChildIndex(startModule);
                      }
            

             

            However, the objects won't load on the stage. Do you know that is?

            • 3. Re: Accessing Components From the MXML in the AS
              skider Level 1

              Just glancing through it, I can't see any reason why the code you've posted shouldn't work. Are you sure it's being called?

               

              I would advise overriding the createChildren method, and putting the child creation code in there. Flex runs that function automatically when the component is ready to be populated with children. If you do that, make sure to call super.createChildren() somewhere in the function.

               

              Other than that, I'm not sure what to tell you.