对定制类的数据绑定

默认的,定制类的实例是不执行数据绑定的。要执行数据绑定,必须使用[Bindable]元标签来告诉编译器。可将[Bindable]用于一个类
一个属性
一个内含的getter方法
一个内含的setter方法
在类声明前使用[Bindable]表明该类所有的public属性和所有的getter和setter方法对是可绑定的。
在属性声明前使用[Bindable]只表明这个属性是可绑定的。
当有getter和setter方法为一样的名称时,只需在它们之一前放置[Bindable],表明它们是可绑定的。
而仅有getter方法时,它只能作为数据绑定的源
而仅用setter方法时,它只能作为数据绑定的目标。
在实际中使用数据绑定时,你不仅要在类定义中声明[Bindable],也要对它的实例声明[Bindable]。
定制的数据绑定
数据绑定实际上是通过标准Flash Player事件模型工作的。分发的事件类型是propertyChange。在很多情况下,能很好地工作。但若一个类有很多执行数据绑定的属性,将会引起低效率的,因为任何时候,任何属性的改变,监听该实例被绑定属性所有的改变的所有监听器都会收到一个通知,如果每个属性分发唯一的事件名,将会有效率的多。可以给[Bindable]标签加上事件名来实现:
[Bindable(event="customEvent")]
但此时,你必须手动分发这个事件:
dispatchEvent(new Event("customEvent"));
而且显然,此时你必须分别为每个要绑定的属性或getter/setter方法对指定[Bindable]。
Example 12-5. Customized data binding
package com.oreilly.programmingflex.binding {
import flash.events.Event;
import flash.events.EventDispatcher;
public class CustomizedDataBindableExample extends EventDisaptcher {
private var _a:String;
private var _b:String;

[Bindable(event="aChange")]
public function get a( ):String {
return _a;
}
public function set a(value:String):void {
_a = value;
dispatchEvent(new Event(A_CHANGE));
}
[Bindable(event="bChange")]
public function get b( ):String {
return _b;
}
public function set b(value:String):void {
_b = value;
dispatchEvent(new Event(B_CHANGE));
}
public function CustomizedDataBindableExample( ) {
_a = "a";
_b = "b";
}
}
}
数据绑定的例子:
一,动态改变Image控件的source,透明度,长宽等
二,把数据绑定用于web service的参数和结果
绑定web service的结果到文本框:
<mx:WebService id="survey" wsdl="http://www.rightactionscript.com/webservices/
FlashSurvey.php?wsdl">
<mx:operation name="getAverages" resultFormat="object" />
</mx:WebService>
<mx:Binding source="survey.getAverages.lastResult.flash"
destination="resultFlash.text" />
<mx:Binding source="survey.getAverages.lastResult.actionscript"
destination="resultActionScript.text" />
将控件的值绑定到web service的参数:
<mx:WebService id="survey" wsdl="http://www.rightactionscript.com/webservices/
FlashSurvey.php?wsdl">
<mx:operation name="getAverages" resultFormat="object" />
<mx:operation name="takeSurvey">
<mx:request>
<years_flash></years_flash>
<years_actionscript></years_actionscript>
</mx:request>
</mx:operation>
</mx:WebService>
<mx:Binding source="flashValue.value"
destination="survey.takeSurvey.request.years_flash" />
<mx:Binding source="actionScriptValue.value" destination="survey.takeSurvey.
request.years_actionscript" />
需要学习的是Image控件的使用及web service的知识。
使用数据绑定代理
一些类型的对象和一些元素是不能直接使用数据绑定的,例如,组件的风格。但可使用一种叫delegation的方法。接下去的内容和例子没完全看懂,在p284。对共享对象使用绑定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值