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>**
4

1 回答 1

0

问题似乎在这里:

     var selectedItemObject:SelectComponet = new SelectComponet();

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

您不是从实际组合框中检索值,而是从函数中创建的另一个隐藏组合框

您应该使用“comb2.comb.selectedItem”而不是“selectedItemObject.comb.selectedItem”

祝你今天过得愉快 !

于 2012-07-06T12:53:44.210 回答