2012-07-06 46 views
0

我有一段代碼從SelectComponent.mxml文件中讀取組合框中的值,並且通過使SelectComponet對象在Main.mxml文件中讀取組件,請檢查Main.mxml中的代碼我只在SelectComponent.mxml中獲得輸出的第一個值,無論我從該組合框中選擇的值是多少,我只能得到與第一個值相對應的輸出,請告訴我如何才能獲得與該值相對應的引用。Combobox實例不工作

this file is Main.mxml 
**<?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:views="Views.*" 
      xmlns:mx="library://ns.adobe.com/flex/mx" 
      minWidth="955" minHeight="600"> 
<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 
<fx:Script> 

    <![CDATA[ 




     import Model.SampleModel; 

     import Views.SelectComponet; 

     import flash.utils.getDefinitionByName; 

     import mx.controls.*; 
     import mx.core.Repeater; 
     import mx.core.UIComponent; 

     var mode:SampleModel = SampleModel.getInstace(); 
     var model = ["Paritosh","Akhil","Chirag","Suresh"]; 
     private function generateComponent(e:MouseEvent):void{ 


     var selectedItemObject:SelectComponet = new SelectComponet(); 

     var name1:String = selectedItemObject.comb.selectedItem.toString(); 
     mx.controls.Alert.show(name1); 
     var val:String = "mx.controls."+name1; 
     var cls:Class = getDefinitionByName(val) as Class; 
     var instance:UIComponent = new cls(); 
     mode.selectedComponent = instance.className; 

     if (instance) { 
      switch (instance.className) { 
       case "ComboBox": 
        ComboBox(instance).dataProvider = model; 
        v2.removeAllChildren(); 
        v2.addChild(instance); 

         break; 
       case "CheckBox": 
         v2.removeAllChildren(); 

        for(var i:int=0;i<model.length;i++){ 
        var instance:UIComponent = new cls(); 
        CheckBox(instance).label = model[i]; 
        v2.addChild(CheckBox(instance)); 
        } 
        break; 
       case "List": 
        List(instance).dataProvider = model; 
        v2.removeAllChildren(); 
        v2.addChild(instance); 
        break; 
       case "DataGrid": 
        DataGrid(instance).dataProvider = model; 
        v2.removeAllChildren(); 
        v2.addChild(instance); 
        break; 
      } 


     } 
     } 
    ]]> 
</fx:Script> 
<mx:ApplicationControlBar width="946" height="44"> 
    <mx:Label text="Dynamic Component Generation"/> 

</mx:ApplicationControlBar> 


<mx:HDividedBox x="10" y="52" width="935" height="409"> 
    <mx:VBox id="v1" width="350" height="447" horizontalScrollPolicy="on" 
      verticalScrollPolicy="off"> 
     <mx:Label text="Select Output Format"/> 
     <views:SelectComponet id = "comb2"/> 
     <mx:ComboBox id="comb1"> 

      <mx:dataProvider> 
       <fx:String>CheckBox</fx:String> 
       <fx:String>ComboBox</fx:String> 
       <fx:String>List</fx:String> 
       <fx:String>DataGrid</fx:String> 
      </mx:dataProvider> 
     </mx:ComboBox> 



    <mx:Button id="butt" label="Get Output"  click="generateComponent(event)"/> 

    </mx:VBox> 
    <mx:VBox id="v2" width="350" height="447" horizontalScrollPolicy="on" 
      verticalScrollPolicy="off"> 

    </mx:VBox> 
</mx:HDividedBox> 

    </s:Application>** 

    and this is SelectComponent.mxml 

    **<?xml version="1.0" encoding="utf-8"?> 
    <s:Group 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="400" height="300"> 
<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 



    </fx:Declarations> 
    <fx:Script> 
    <![CDATA[ 
      import Model.SampleModel; 
      private var model:SampleModel = SampleModel.getInstace(); 

    ]]> 
    <mx:ComboBox id="comb"> 

    <mx:dataProvider> 
     <fx:String>ComboBox</fx:String> 
     <fx:String>CheckBox</fx:String> 
     <fx:String>List</fx:String> 
     <fx:String>DataGrid</fx:String> 
    </mx:dataProvider> 
    </mx:ComboBox> 

    </s:Group>** 
+0

它接縫不足代碼。你可以創建樣本併發布,以便我們提供幫助。 – 2012-07-06 10:44:48

回答

0

這個問題似乎是在這裏:

 var selectedItemObject:SelectComponet = new SelectComponet(); 

    var name1:String = selectedItemObject.comb.selectedItem.toString(); 

你不是retreving從actuel compobox的價值,但是從功能創造了另一個隱藏compobox

您應該使用「comb2.comb.selectedItem」而不是「selectedItemObject.comb.selectedItem」

祝您有美好的一天!