從XML

2010-02-02 63 views
2

Flex的顏色拾取負荷顏色我有我想要從外部XML加載自己的顏色的顏色拾取,我的XML看起來像這樣從XML

<colors> 
     <color label="Aqua" colorHex="0xFFFFCC"/> 
     <color label="Forest Green" colorHex="0xCCFF00"/> 
     <color label="Kelly Green" colorHex="0xCCFF00"/> 
     <color label="Royal Blue" colorHex="0xCCFF00"/> 
     <color label="Columbia Blue" colorHex="0xCCFF00"/> 
     <color label="Navy Blue" colorHex="0xCCFF00"/> 
     <color label="Purple" colorHex="0xCCFF00"/> 
     <color label="Maroon" colorHex="0xCCFF00"/> 
     <color label="Cardinal" colorHex="0xCCFF00"/> 
     <color label="Garnet" colorHex="0xCCFF00"/> 
     <color label="Red" colorHex="0xCCFF00"/> 
     <color label="Orange" colorHex="0xCCFF00"/> 
     <color label="Texas Orange" colorHex="0xCCFF00"/> 
     <color label="Light Gold" colorHex="0xCCFF00"/> 
     <color label="Vega Gold" colorHex="0xCCFF00"/> 
     <color label="Brown" colorHex="0xCCFF00"/> 
     <color label="Black" colorHex="0xCCFF00"/> 
     <color label="Silver" colorHex="0xCCFF00"/> 
     <color label="White" colorHex="0xCCFF00"/> 
     <color label="Pink" colorHex="0xCCFF00"/> 
    </colors> 

所以我加載XML,加入到與綁定變量XML類型和添加對ColorPicker的這樣

[Bindable] private var colorsDp:XML; 

colorsDP = XML(loader.data);// the loader is an object that has the xml loaded 

<mx:ColorPicker id="cp" dataProvider="{colorsDp}" labelField="label" colorField="colorHex" editable="false"/> 

的文檔說,我可以使用爲數據提供器爲XML,數組,ArrayCollection的,xmlListColletion等的dateprovider,由於DataProvider轉換爲數據的ICollectionView ,但不起作用,爲什麼?

+0

你確定它確實正在加載? – 2010-02-02 23:32:03

回答

0

我能夠通過將ColorPicker的dataProvider綁定到一個對象數組來獲得它的工作。

的MXML和ActionScript代碼:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
       initialize="init(event)" 
       layout="absolute" > 

    <mx:Script> 
      <![CDATA[ 
       import mx.collections.ArrayCollection; 
       import mx.collections.XMLListCollection; 
       import mx.controls.Alert; 
       import mx.rpc.events.FaultEvent; 
       import mx.rpc.events.ResultEvent; 

       private var colors:XMLListCollection; 
       [Bindable] private var colorsArray:Array; 

       private function init(evt:Event):void 
       { 
        XMLService.send(); 
       } 

       private function xmlService_result(evt:ResultEvent):void 
       { 
        colors = new XMLListCollection(evt.result.color); 
        colorsArray = new Array(colors.toArray()); 
        var numOfColors:int = colors.length; 
        for (var i:uint = 0; i < numOfColors; ++i) 
        { 
         var colorXML:XML = colors.getItemAt(i) as XML; 
         var colorObject:Object = new Object(); 
         colorObject.name = colorXML.attribute('label'); 
         colorObject.value = colorXML.attribute('colorHex'); 
         colorsArray.push(colorObject); 
        } 

       } 

       private function xmlService_fault(evt:FaultEvent):void 
       { 
        Alert.show('Failed to load external XML data:\n'+evt.fault.faultString); 
       } 

      ]]> 
    </mx:Script> 

    <mx:HTTPService id="XMLService" 
        url="colors.xml" 
        result="xmlService_result(event)" 
        resultFormat="e4x" 
        fault="xmlService_fault(event)" /> 

    <mx:ColorPicker id="colorPicker" 
        colorField="value" 
        labelField="name" editable="false" 
        dataProvider="{colorsArray}"/> 

</mx:Application> 

爲了這個工作,你必須有一個名爲colors.xml在同一目錄中應用程序的主MXML文件的XML文件。我對colors.xml的標記如下:

<?xml version="1.0" encoding="utf-8"?> 
<colors> 
    <color label="Aqua" colorHex="0xFFFFCC"/> 
    <color label="Forest Green" colorHex="0x228B22"/> 
    <color label="Kelly Green" colorHex="0x00A000"/> 
    <color label="Royal Blue" colorHex="0x4169E1"/> 
    <color label="Columbia Blue" colorHex="0x75B2DD"/> 
    <color label="Navy Blue" colorHex="0x23238E"/> 
    <color label="Purple" colorHex="0x800080"/> 
    <color label="Maroon" colorHex="0x800000"/> 
    <color label="Cardinal" colorHex="0xC41E3A"/> 
    <color label="Garnet" colorHex="0xb3005b"/> 
    <color label="Red" colorHex="0xFF0000"/> 
    <color label="Orange" colorHex="0xFFA500"/> 
    <color label="Texas Orange" colorHex="0xffa500"/> 
    <color label="Light Gold" colorHex="0xFD9202"/> 
    <color label="Vega Gold" colorHex="0xD9D919"/> 
    <color label="Brown" colorHex="0xA52A2A"/> 
    <color label="Black" colorHex="0x000000"/> 
    <color label="Silver" colorHex="0xC0C0C0"/> 
    <color label="White" colorHex="0xFFFFFF"/> 
    <color label="Pink" colorHex="0xFFC0CB"/> 
</colors> 
+0

謝謝你的回覆,它似乎工作,但我不知道如果轉換這樣的xmllistcollection colorsArray = new Array(colors.toArray()); 是必要的,因爲下面你添加到相同的數組從xmllistcollection創建的對象,但我明白了,謝謝! – goseta 2010-02-04 13:56:34