[英]reading values from xml file in actionscript 3 into a combobox
我在.mxml文件中的布局是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 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="600" height="800" >
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:HBox width="100%" horizontalAlign="right" top="20">
<mx:ComboBox id="comboBox" fontSize="12" width="10%" dataProvider="{middleStack}" name="versioncombo"/>
</mx:HBox>
<mx:VBox top="50" left="10" >
<mx:Label text="Builds" fontSize="18">
</mx:Label>
</mx:VBox>
<mx:VBox top="50" horizontalAlign="center" left="200">
<mx:Label text="Date" fontSize="18">
</mx:Label>
</mx:VBox>
<mx:VBox top="50" horizontalAlign="center" right="10">
<mx:Label text="Release" fontSize="18">
</mx:Label>
</mx:VBox>
<mx:VBox height="100%" width="100%" styleName="centerLightGreyBg" verticalGap="0">
<mx:ViewStack id="middleStack" width="100%" height="100%" backgroundAlpha="0" change="changev()">
</mx:ViewStack>
</mx:VBox>
<fx:Style>
現在,我想通過從XML文件中讀取值,將值添加到id = combobox和name = versioncombo的組合框中。 我的xml是這樣的。
<Install>
<version>
<number>5.0</number>
<build>907681</build>
<path></path>
</version>
<version>
<number>6.2</number>
<build>1043305</build>
<path></path>
</version>
</Install>
我想用版本號即5.0、6.2填充組合框。
我嘗試點擊此鏈接,但無法執行此操作http://www.parorrey.com/blog/flash-development/as3-adding-dropdown-combobox-flash-component-using-actionscript-with-xml-data /
我是AIR的新手。
我這樣嘗試過:-
import fl.data.DataProvider;
var country:String='';
var countriesList:Array = [];
//URLLoader class helps you to load data from an external source such as a URL
var xmlLoader:URLLoader ;
var xmlData:XML = new XML();
public function Pathfinding() {
// constructor code
xmlLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("grid.xml"));
}
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
xmlData.ignoreWhite = true;
ParseCountries(xmlData);
//trace(xmlData);
}
function ParseCountries(countriesInput:XML):void {
var countriesListing:XMLList = countriesInput.country.attributes();
var i:int;
for each (var countryName:XML in countriesListing) {
//populate the countriesList array to be used with ComboBox
countriesList.push( {label:countryName, data:countryName} );
trace(countryName);
}
versioncombo.prompt = "Select version:";
versioncombo.dataProvider = new DataProvider(countriesList);
versioncombo.addEventListener(Event.CHANGE, dropDownHandler);
versioncombo.dropdown.rowHeight = 30;
versioncombo.width = 150;
}
function dropDownHandler(event:Event):void {
country = ComboBox(event.target).selectedItem.data;
trace("selected country: "+ country);
//do something here whatever you want
}
comboBox.prompt = "Select Country:";
comboBox.dataProvider = new DataProvider(countriesList);
comboBox.addEventListener(Event.CHANGE, dropDownHandler);
comboBox.dropdown.rowHeight = 30;
comboBox.width = 150;
但出現錯誤“ -1180:調用可能未定義的方法DataProvider”。
comboBox.dataProvider = new DataProvider(countriesList);
不為我工作。
您正在將name
屬性與定義的ComboBox
組件的id
屬性混淆。 這是您需要用來引用ComboBox
實例的id
。
來自使用組件 :
您應該為每個動態創建的組件聲明一個實例變量,並在其中存儲對新創建的組件的引用,就像為組件實例標記設置id屬性時MXML編譯器所做的那樣。 然后,您可以以與在MXML中聲明創建的組件相同的方式訪問動態創建的組件。
您只需要閱讀有關 dataprovider正確工作的信息。 您不需要創建新的實例,例如“ new DataProvider”。 請使用ArrayCollection或IList中的任何一個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.