技術分享 在ActionScript中如何定義Flex數據綁定
你對在ActionScript 中定義Flex數據綁定是否了解,這里和大家分享一下,通過使用bindProperty() 方法可以讓我們創建一個到用變量實現的屬性的數據綁定,或者用bindSetter()方法創建一個到用方法實現的屬性的Flex數據綁定。
在ActionScript 中定義Flex數據綁定
通過使用mx.binding.utils.BindingUtils能夠在ActionScript中定義綁定。這個類定義了幾個靜態方法,通過使用bindProperty() 方法可以讓我們創建一個到用變量實現的屬性的數據綁定,或者用bindSetter()方法創建一個到用方法實現的屬性的Flex數據綁定。
(1)在MXML 與在ActionScript 定義Flex數據綁定的區別
編譯期在MXML 中定義Flex數據綁定與在運行期在ActionScript 中定義Flex數據綁定有一些不同之處:
◆不能在由bindProperty()或者bindSetter()方法定義綁定表達式中引入ActionScript 代碼。相反,使用bindSetter()方法可以指定一個在綁定發生時調用的
方法。
◆ 不能在由ActionScript 中定義的綁定表達式中引入E4X 表達式。
◆ 在由the bindProperty()或者bindSetter()方法定義的Flex數據綁定表達式的屬性鏈中不能引入函數或者數組元素。更多信息見Working with bindable property chains.
◆同運行時使用bindProperty()或者bindSetter()定義的Flex數據綁定相比,MXML 編譯器有更好的警告和錯誤檢查支持。
(2)范例:在ActionScript 中定義Flex數據綁定
下面的例子是用bindSetter()建立了一個Flex數據綁定。bindSetter()方法的參數設置如下:
◆ 源(source) 對象
◆ 源(source) 屬性名
◆ 當源(source)屬性變化被調用的方法。
下面的范例中,當向TextInput 控件中輸入文本時,文本會被轉換為大寫形式并拷貝給TextArea
控件:
- <?xml version="1.0"?>
- <!-- binding/BindSetterAS.mxml -->
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
- <mx:Script>
- <![CDATA[
- import mx.binding.utils.*;
- import mx.events.FlexEvent;
- // Method called when myTI.text changes.
- public function updateMyString(val:String):void {
- myTA.text = val.toUpperCase();
- }
- <!-- Event listener to configure binding. -->
- public function mySetterBinding(event:FlexEvent):void {
- var watcherSetter:ChangeWatcher =
- BindingUtils.bindSetter(updateMyString, myTI, "text");
- }
- ]]>
- </mx:Script>
- <mx:Label text="Bind Setter using setter method"/>
- <mx:TextInput id="myTI"
- text="Hello Setter" />
- <mx:TextArea id="myTA"
- initialize="mySetterBinding(event);"/>
- </mx:Application>
(3)定義綁定觀察者 (watchers)
Flex 有個mx.binding.utils.ChangeWatcher 類,可以用這個類來定義一個Flex數據綁定觀察者。通常,Flex數據綁定觀察者在綁定發生時激活一個事件監聽器。可按照下面的范例使用
ChangeWatcher 的watch()即可建立一個Flex數據綁定觀察者:
- <?xml version="1.0"?>
- <!-- binding/DetectWatcher.mxml -->
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- initialize="initWatcher();">
- <mx:Script>
- <![CDATA[
- import mx.binding.utils.*;
- import mx.events.FlexEvent;
- import mx.events.PropertyChangeEvent;
- public var myWatcher:ChangeWatcher;
- // Define binding watcher.
- public function initWatcher():void {
- // Define a watcher for the text binding.
- ChangeWatcher.watch(textarea, "text", watcherListener);
- }
- // Event listener when binding occurs.
- public function watcherListener(event:Event):void {
- myTA1.text="binding occurred";
- // Use myWatcher.unwatch() to remove the watcher.
- }
- ]]>
- </mx:Script>
- <!-- Define a binding expression_r to watch. -->
- <mx:TextInput id="textinput" text="Hello"/>
- <mx:TextArea id="textarea" text="{textinput.text}"/>
- <!-- Trigger a binding. -->
- <mx:Button label="Submit" click="textinput.text='Goodbye';"/>
- <mx:TextArea id="myTA1"/>
- </mx:Application>
上面的范例中,為Flex數據綁定觀察者定義了事件監聽器,在這個事件監聽器中使用了單個參數來包含事件對象。事件對象的數據類型由被觀察的屬性所決定。每個可綁定的屬性會不同的
事件類型以及相關的事件對象。有關確定事件類型的更多信息見“使用Bindable 元數據標記”。
【編輯推薦】