一 , DataModel(数据类)

①:需要继承 bind.BaseBindModel(为了发送属性数据)

②:需要监听的数值需要写setter/getter

③:在setter中使用changeValue方法

changeValue方法参数

1,属性名称

2,与属性对应的字段名称

3,值(要赋的值)

如:

moduleapp{/***数据类-成员需要绑定*/exportclassDataTestextendsbind.BaseBindModel{private_name:string="Kayer";private_combat:number=1;/***设置名称*/publicsetName($name:string){this.changeValue<string>("Name","_name",$name);}/***获取名称*/publicgetName():string{returnthis._name;}publicsetCombat($combat:number){this.changeValue<number>("Combat","_combat",$combat);}publicgetCombat():number{returnthis._combat;}publicconstructor(){super();}}}


二,在View(获取他地方)绑定数值

①:绑定需要使用bind.BindTool类(为了获得发送的属性数据并更新)

②:绑定方案有2种

1,属性绑定 : 直接将新值赋给绑定的值

静态方法 bindProperty<T>

参数5( 最后一个参数 ) : 是否马上用DataModel里面的值为View赋值,默认true

2,回调方法绑定 :

a,回调方法参数为 IBindEventData<T>

静态方法 bindCallBack<T>

参数4(最后一个参数):是否马上用DataModel里面的值为View赋值,默认true

③:销毁

bindProperty<T> 和 bindCallBack<T> 都会返回类 : Bind2Subscriber<T>

Bind2Subscriber<T>提供了销毁方法 : destory(),不需要监听(view关闭时)调用一下

如:

/***数据类-成员需要绑定*/exportclassDataTestextendsbind.BaseBindModel{private_name:string="Kayer";private_combat:number=1;/***设置名称*/publicsetName($name:string){this.changeValue<string>("Name","_name",$name);}/***获取名称*/publicgetName():string{returnthis._name;}publicsetCombat($combat:number){this.changeValue<number>("Combat","_combat",$combat);}publicgetCombat():number{returnthis._combat;}publicconstructor(){super();}}}二,在View(获取他地方)绑定数值①:绑定需要使用bind.BindTool方法②:绑定方案有2种1,属性绑定:直接将新值赋给绑定的值静态方法bindProperty<T>参数5(最后一个参数):是否马上用DataModel里面的值为View赋值,默认true2,回调方法绑定:a,回调方法参数为IBindEventData<T>静态方法bindCallBack<T>参数4(最后一个参数):是否马上用DataModel里面的值为View赋值,默认true③:销毁bindProperty<T>和bindCallBack<T>都会返回类:Bind2Subscriber<T>Bind2Subscriber<T>提供了销毁方法:destory(),不需要监听(view关闭时)调用一下如:privatevName:string="CCCC";privatevCombat:number=0;privatedataTest:DataTest=null;privatedBind:bind.Bind2Subscriber<string>=null;//不用时需要销毁privatedBind2:bind.Bind2Subscriber<number>=null;//不用时需要销毁publicconstructor(){super();this.skinName="resource/eui_skins/ButtonDemo.exml";egret.log("initvName:"+this.vName);this.dataTest=newDataTest();egret.log("=======绑定字段(属性)======");this.dBind=bind.BindTool.bindProperty(this,"vName",this.dataTest,"Name",true);egret.log("initChangevName:"+this.vName);this.dataTest.Name="Aonaufly";egret.log("Changed2ListenervName:"+this.vName);egret.log("=======绑定回调方法======");egret.log("initvCombat:"+this.vCombat);this.dBind2=bind.BindTool.bindCallBack(this.bindCallBack,this.dataTest,"Combat",true);this.dataTest.Combat=7;}privatebindCallBack($data:bind.IBindEventData<number>):void{if($data.$oldValue==undefined){//初始化值egret.log("initChangevCombat:"+$data.$newValue);}else{//监听值egret.log("Changed2ListenervCombet:");egret.log("oldValueis:"+$data.$oldValue);egret.log("newValueis:"+$data.$newValue);}this.vCombat=$data.$newValue;//赋新值}/***销毁*/publicdestory():void{if(this.dBind!=null){this.dBind.destory();this.dBind=null;}if(this.dBind2!=null){this.dBind2.destory();this.dBind2=null;}}

结果:

附件:http://down.51cto.com/data/2368703