接着上一篇继续讲:
数据绑定除了用[Bindable]标签来申明以外,也可以用<mx:Binding/>组件和ActionScript实现。
用<mx:Binding/>,例如:
<mx:binding source="text1.text" destination="text2.text"/>
<mx:binging source="text2.text" destination="text1.text"/>
<mx:TextInput id="text1"/>
<mx:TextInput id="text2"/>
source为绑定源,destination为目的源,按上面的写法,不论是text1还是text2发生变化,都会引起对方的变化。细心的你也许会想这不会引起死循环吗?答案是否定的,不会引起死循环。我想应该是Flex内部的机制已经优化了这种情况,让其只触发一次。
用ActionScript来绑定:
1.使用bindProperty()。
bindProperty(site:Object, prop:String,host:Object, chain:Object,commitOnly:Boolean = false):ChangeWatcher,例如:
var myc:ChangeWatcher=BindingUtils.bindProperty(text2,"text",text1,"text");
即当text1的值发生变化时text2也跟着变,site为目的对象,prop为目的属性,host为绑定源,china为绑定源属性链——关于属性链下面再讲,commitOnly默认为False,即不管是确认事件还是未确认事件都将触发绑定,而为True时,只有确认事件才能触发绑定,这个一般用不到,和Flex自身的事件机制有关,如果为false的话,当数据改变时将触发两次绑定事件,当为True时,只触发一次,自己可以用bindSetter方法来做测试。当不想绑定时可以用myc.unwatch()方法来解除绑定。
2.使用bindSetter()。
bindSetter(setter:Function, host:Object, chain:Object,commitOnly:Boolean = false):ChangeWatcher,例如:
var myc:ChangeWatcher=BindingUtils.bindSetter(change,text1,"text",true);
private function change(str:String):void
{
text2.text=str;
}
change就是当绑定源发生变化时所触发的函数,其他参数都一样。
3.使用ChangeWatcher.watch()。
同样可以用ChangeWatcher.watch方法来监控对象属性的变化,非常有用。
watch(host:Object, chain:Object,handler:Function,commitOnly:Boolean = false):ChangeWatcher,例如:
var myc:ChangeWatcher=ChangeWatcher.watch(text1,"text",change);
private function change(e:Event):void
{
text2.text=text1.text;
}
这里的Event和绑定数据所定义的触发事件有关,你可以用所有事件的父类Event来表示。
注意:
as主要是通过mx.binding.utils.BindingUtils 这个类来实现数据绑定,用MXML和as实现数据绑定有以下几点不同:
1.当使用AS做数据绑定时,bindProperty()或 bindSetter()方法中不能使用AS代码,这和MXML是不同的, 可以用bindSetter() 方法来申明一个绑定处理函数。
2.当使用AS做数据绑定时,同样不能使用EX4语法,也就是说不能直接使用XML解析语法了。
3.当使用AS做数据绑定时,在属性链中不能使用任何函数和数组。
4.MXML具有更好的错误提示和警告功能。
最后来讲讲属性链。
属性链就是bindProperty()和 bindSettet()等方法中的chain参数所表示的对象, 有时也许绑定源并不只是text1.text这样的简单形式,也可以是类似于user.name.text1.text,那么就存在一个关系链的问题,如果这条链中的某一项发生了改变,会不会触发绑定呢?答案是如果你想让其改变其中的某一项都能触发数据绑定,那么这条链的每个元素必须是可绑定的。对于上面的这种形式,可以这样使用bindProperty方法:
bindProperty(text2, "text", this, ["user", "name","text1","text"])。
就这么多吧,主要还是靠自己研究,希望对你有所帮助
原文地址:
分享到:
相关推荐
Data BindIng简单的说就是当绑定源属性发生变化时,Flex 会把绑定源变化后属性的值赋给目的物的属性。做到了数据同步。
Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET By Brian Noyes ............................................... Publisher: Addison Wesley ...
Android Data Binding结合RecyclerView的使用
This book is all about the what and the why of binding to data sources in a Windows Forms application built using Visual Studio 2005. The book goes into great detail in explaining the rationale behind...
These tutorials describe how to map your classes to your tables manually (rather than with an automated tool like SqlMetal) so that you can have support for M:M relationships and data binding against ...
Brian Noyes is a software architect, trainer, writer, and speaker with IDesign (www.idesign.net), a premier .NET architecture and design consulting and training company. He has been developing ...
豆瓣电影搜索 利用Data Binding在RecycleView展示电影列表
Data Binding with Windows Forms 2.0 Programming Smart Client Data Applications with .NET
Android Data Binding实战-入门篇学习笔记,包含实例代码。 基于 Android Studio平台。
In this chapter, we examine data binding, the ability to associate non-GUI objects that hold and maintain data with the controls that present and receive that data
AndroidDataBindingExample, Android Data Binding 代码实战
In this chapter, we examine data binding, the ability to associate non-GUI objects that hold and maintain data with the controls that present and receive that data
本文主要介绍Android Data Binding数据绑定的知识,这里整理了详细的资料及简单示例代码帮助大家学习理解此部分知识,有需要的小伙伴可以参考下
主要介绍了Android Data Binding 在 library module 中遇到错误及解决办法的相关资料,需要的朋友可以参考下
Android Data Binding Library 官方文档中文翻译,出自https://blog.csdn.net/jjwwmlp456/article/details/54915981的博客,被我制作成离线版pdf以供保存, Google官方文档地址:...
Approaches and Best Practices in Web Service Style, Data Binding and Validation.pdf
微软的示例! VB.net的 数据绑定!
Android Data Binding Adapter for ListView and RecyclerView..zip,使用新的android数据绑定框架将集合绑定到listviews和recyclerviews的简单方法
Data Binding Validator by Ilhasoft The Data Binding Validator makes it easy and quick to validate fields in forms using data binding framework. Download Step 1: Add it in your root build.gradle at ...