从actionscript 3中的xml文件读取值到组合框中

时间:2016-02-06 12:40:03

标签: xml actionscript-3 flash 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/

AS3 Procedural. Randomise order of Items in Combobox From XMl

我是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;

但是收到错误&#34; -1180:调用可能未定义的方法DataProvider。&#34;

                comboBox.dataProvider = new DataProvider(countriesList); 

不适合我。

2 个答案:

答案 0 :(得分:1)

您将name属性与您定义的id组件的ComboBox属性混淆。您需要使用id来引用ComboBox实例。

来自Working with Components

  

您应该为每个动态创建的实例变量声明一个实例变量   组件并存储对其中新创建的组件的引用,   正如MXML编译器在为a设置id属性时所做的那样   组件实例标签。然后,您可以访问动态创建的   组件的方式与MXML中声明性创建的组件相同。

答案 1 :(得分:1)

您需要read about正确使用dataprovider。您不需要创建新实例,例如“new DataProvider”。请使用 ArrayCollection 或任何 IList