2011-03-13 56 views
0

我想通過mxml(而不是動作)獲取數組的長度。我有以下內容:獲取更新的數組長度

<s:Application 
xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx" 
> 


<fx:Script> 
    <![CDATA[ 


     [Bindable] 
     public var rivers:Array = ["Nile", "Amazon", "Yangtze", "Mississippi"]; 



     protected function remove(event:MouseEvent):void 
     { 
      rivers.pop(); 
      test2.text = String(rivers.length); 

     } 

    ]]> 
</fx:Script> 

<s:VGroup> 

<mx:Text id="test1" text="{rivers.length}" color="red"/> 
<mx:LinkButton label="remove Item" click="remove(event)" /> 

<mx:Text id="test2" color="blue"/> 

</s:VGroup> 

</s:Application> 

爲什麼test2顯示正確的數組長度但test1已過時?

回答

1

您使用綁定來顯示test1中的河流長度屬性。 事情是你的收藏是一個數組。 當數組發生更改時,它不派遣事件,這就是爲什麼從不觸發綁定的原因。

改爲使用ArrayCollection實例。

0

嘗試這樣:

[Bindable] 
    var len:String = "0"; 
    [Bindable] 
    public var rivers:ArrayCollection = ["Nile", "Amazon", "Yangtze", "Mississippi"]; 

    <mx:Script> 
     <![CDATA[ 
       protected function remove(event:MouseEvent):void { 
        rivers.removeItemAt(0); 
        len = String(rivers.length); 

       } 
     ]]> 
    </mx:Script> 

    <mx:Text id="test1" text="{len}" color="red"/> 
    <mx:LinkButton label="remove Item" click="remove(event)" /> 

    <mx:Text id="test2" color="blue"/>