2011-10-25 11 views
0

私は、TabNavigatorでタブを動的に作成するアプリケーションがあります。私のコードに従って見ることができるように、私は基本的に特定の名前の5つのタブを持っています。今では、タブと同じ名前の5つのmxmlコンポーネントもあります(つまり、mxmlコンポーネントの名前はTab1、Tab2などと同じです)。非動的な方法は、myTab1を使用することです。Tab1 = new Tab1(); myTab1:Tab2 =新しいTab2);したがって、私は欲しくないタブごとにこれを行う必要があります。 私がしたいのは、Arrayをループしている間に各タブにmxmlコンポーネントをロードすることです。私は十分にはっきりしていると思います。Flexで動的にコンポーネントを読み込む4

<?xml version="1.0" encoding="utf-8"?> 
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> 
     <fx:Script> 
      <![CDATA[ 
       import mx.containers.VBox; 
       import mx.controls.Label; 
       import mx.events.FlexEvent; 
       import spark.components.NavigatorContent; 


       protected function tabNavigator_creationCompleteHandler(event:FlexEvent):void 
       { 

        var superModules:Array =["Tab1","Tab2","Tab3","Tab4","Tab5"]; 
        for each (var superModule in superModules) 
        { 
         var myNavigatorContent:NavigatorContent = new NavigatorContent(); 
         myNavigatorContent.percentHeight = 100; 
         myNavigatorContent.percentWidth = 100; 
         myNavigatorContent.label = superModule; 
         myNavigatorContent.addChild(superModule as DisplayObject); 
             tabNavigator.addChild(myNavigatorContent); 

        } 


       } 

      ]]> 
     </fx:Script> 
     <fx:Declarations> 
      <!-- Place non-visual elements (e.g., services, value objects) here --> 
     </fx:Declarations> 
     <mx:TabNavigator id="tabNavigator" width="100%" height="100%" creationComplete="tabNavigator_creationCompleteHandler(event)"> 
     </mx:TabNavigator> 
    </s:Application> 

    Can somebody help on achieving this. 
    Many thanks. 

答えて

1

はい、私の解決策を見つけることができます。

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> 
<fx:Script> 
    <![CDATA[ 
     import mx.containers.VBox; 
     import mx.controls.Label; 
     import mx.core.IVisualElement; 
     import mx.core.UIComponent; 
     import mx.events.FlexEvent; 

     import spark.components.NavigatorContent; 

     /*The class "Tab1","Tab2" not compiled with the application 
     because the linker and the compiler do not add classes 
     that are not referenced in the code. 

     Even though you’ll never use the _dummyVarToAddTabToAppCompilation1 
     variable it is necessary to use that line to instruct the compiler 
     to include Tab1,Tab2 in the compilation.*/ 

     private var _dummyVarToAddTabToAppCompilation1:Tab1; 
     private var _dummyVarToAddTabToAppCompilation2:Tab2; 

     protected function tabNavigator_creationCompleteHandler(event:FlexEvent):void 
     { 

      var superModules:Array =["Tab1","Tab2"]; 
      for each (var superModule in superModules) 
      { 
       var myNavigatorContent:NavigatorContent = new NavigatorContent(); 
       myNavigatorContent.percentHeight = 100; 
       myNavigatorContent.percentWidth = 100; 
       myNavigatorContent.label = superModule; 
       // Convert class name to Class object. 
       var cls:Class = getDefinitionByName(superModule) as Class; 
       // Create a new instance of the class. 
       var instance:UIComponent = new cls(); 
       myNavigatorContent.addElement(instance); 
       tabNavigator.addChild(myNavigatorContent); 
      } 
     } 

    ]]> 
</fx:Script> 
<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 
<mx:TabNavigator id="tabNavigator" width="100%" height="100%" creationComplete="tabNavigator_creationCompleteHandler(event)"> 
</mx:TabNavigator> 

関連する問題