我有一段代码从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 :(得分:0)
问题似乎在这里:
var selectedItemObject:SelectComponet = new SelectComponet();
var name1:String = selectedItemObject.comb.selectedItem.toString();
你没有从initel compobox中恢复值,而是从函数中创建的另一个隐藏的compobox
您应该使用“comb2.comb.selectedItem”而不是“selectedItemObject.comb.selectedItem”
度过美好的一天!