flex中,属性上标记Bindable,在属性变化时能进行数据绑定.
但是在只读的属性上加这标记,则不会起作用.
比如这个页面
<?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:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
private var bar1:int;
private var bar2:int;
private var bar3:int;
[Bindable]
public function get total():int{
return bar1+bar2+bar3;
}
public function crap():void{
bar1=5;
bar2=10;
bar3=1;
}
]]>
</fx:Script>
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<s:Label text="{total}"/>
<s:Button click="crap();"/>
</s:Application>
会提示
只读 getter 上的 [Bindable] 没有必要,因此将会被忽略
因此需要
[Bindable(event="totalChanged")]
public function get total():int{
return bar1+bar2+bar3;
}
public function crap():void{
bar1=5;
bar2=10;
bar3=1;
this.dispatchEvent(newEvent("totalChanged",true,true));
}
这样就能正常工作了.