温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Egret之属性绑定

发布时间:2020-07-15 21:37:51 来源:网络 阅读:2266 作者:Aonaufly 栏目:开发技术

一 , DataModel(数据类)

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

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

③:在setter中使用changeValue方法

changeValue方法参数

1,属性名称

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

3,值(要赋的值)

如:

module app {
	/**
	 * 数据类-成员需要绑定
	 */
	export class DataTest extends bind.BaseBindModel {
		private _name : string = "Kayer";
		private _combat : number = 1;
		/**
		 * 设置名称
		 */
		public set Name( $name : string ){
			this.changeValue<string>("Name","_name",$name);
		} 
		/**
		 * 获取名称
		 */
		public get Name() : string{
			return this._name;
		}
		public set Combat( $combat : number ){
			this.changeValue<number>("Combat","_combat",$combat);
		}
		public get Combat() : number{
			return this._combat;
		}
		public constructor() {
			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关闭时)调用一下

如:

	/**
	 * 数据类-成员需要绑定
	 */
	export class DataTest extends bind.BaseBindModel {
		private _name : string = "Kayer";
		private _combat : number = 1;
		/**
		 * 设置名称
		 */
		public set Name( $name : string ){
			this.changeValue<string>("Name","_name",$name);
		} 
		/**
		 * 获取名称
		 */
		public get Name() : string{
			return this._name;
		}
		public set Combat( $combat : number ){
			this.changeValue<number>("Combat","_combat",$combat);
		}
		public get Combat() : number{
			return this._combat;
		}
		public constructor() {
			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关闭时)调用一下
如:
		private vName : string = "CCCC";
		private vCombat : number = 0;
		private dataTest : DataTest = null;
		private dBind : bind.Bind2Subscriber<string> = null;//不用时需要销毁
		private dBind2 : bind.Bind2Subscriber<number> = null;//不用时需要销毁
		public constructor() {
			super();
			this.skinName = "resource/eui_skins/ButtonDemo.exml";
			egret.log( "init vName : " + this.vName );
			this.dataTest = new DataTest();
			egret.log( "======= 绑定字段(属性) ======" );
			this.dBind = bind.BindTool.bindProperty(this,"vName", this.dataTest , "Name",true);
			egret.log( "initChange vName : " + this.vName );
			this.dataTest.Name = "Aonaufly";
			egret.log( "Changed2Listener vName : " + this.vName );
			egret.log("======= 绑定回调方法 ======");
			egret.log("init vCombat :" + this.vCombat);
			this.dBind2 = bind.BindTool.bindCallBack( this.bindCallBack , this.dataTest , "Combat" , true );
			this.dataTest.Combat = 7;
		}
		private bindCallBack( $data : bind.IBindEventData<number> ):void{
			if( $data.$oldValue == undefined ){
				//初始化值
				egret.log( "initChange vCombat :" + $data.$newValue );
			}else{
				//监听值
				egret.log( "Changed2Listener vCombet :" );
				egret.log( "oldValue is : " + $data.$oldValue);
				egret.log( "newValue is : " + $data.$newValue );
			}
			this.vCombat = $data.$newValue;//赋新值
		}
		/**
		 * 销毁
		 */
		public destory() : void{
			if( this.dBind != null ){
				this.dBind.destory();
				this.dBind = null;
			}
			if( this.dBind2 != null ){
				this.dBind2.destory();
				this.dBind2 = null;
			}
		}

结果:

Egret之属性绑定

附件:http://down.51cto.com/data/2368703
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI