溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ActionScript中怎么定義Flex數(shù)據(jù)綁定

發(fā)布時(shí)間:2021-08-13 11:58:10 來源:億速云 閱讀:122 作者:Leah 欄目:編程語(yǔ)言

ActionScript中怎么定義Flex數(shù)據(jù)綁定,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

在ActionScript 中定義Flex數(shù)據(jù)綁定

通過使用mx.binding.utils.BindingUtils能夠在ActionScript中定義綁定。這個(gè)類定義了幾個(gè)靜態(tài)方法,通過使用bindProperty() 方法可以讓我們創(chuàng)建一個(gè)到用變量實(shí)現(xiàn)的屬性的數(shù)據(jù)綁定,或者用bindSetter()方法創(chuàng)建一個(gè)到用方法實(shí)現(xiàn)的屬性的Flex數(shù)據(jù)綁定。

(1)在MXML 與在ActionScript 定義Flex數(shù)據(jù)綁定的區(qū)別

編譯期在MXML 中定義Flex數(shù)據(jù)綁定與在運(yùn)行期在ActionScript 中定義Flex數(shù)據(jù)綁定有一些不同之處:
◆不能在由bindProperty()或者bindSetter()方法定義綁定表達(dá)式中引入ActionScript 代碼。相反,使用bindSetter()方法可以指定一個(gè)在綁定發(fā)生時(shí)調(diào)用的
方法。
◆ 不能在由ActionScript 中定義的綁定表達(dá)式中引入E4X 表達(dá)式。
◆ 在由the bindProperty()或者bindSetter()方法定義的Flex數(shù)據(jù)綁定表達(dá)式的屬性鏈中不能引入函數(shù)或者數(shù)組元素。更多信息見Working with bindable property chains.
◆同運(yùn)行時(shí)使用bindProperty()或者bindSetter()定義的Flex數(shù)據(jù)綁定相比,MXML 編譯器有更好的警告和錯(cuò)誤檢查支持。

(2)范例:在ActionScript 中定義Flex數(shù)據(jù)綁定

下面的例子是用bindSetter()建立了一個(gè)Flex數(shù)據(jù)綁定。bindSetter()方法的參數(shù)設(shè)置如下:
◆ 源(source) 對(duì)象
◆ 源(source) 屬性名
◆ 當(dāng)源(source)屬性變化被調(diào)用的方法。
下面的范例中,當(dāng)向TextInput 控件中輸入文本時(shí),文本會(huì)被轉(zhuǎn)換為大寫形式并拷貝給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 有個(gè)mx.binding.utils.ChangeWatcher 類,可以用這個(gè)類來定義一個(gè)Flex數(shù)據(jù)綁定觀察者。通常,F(xiàn)lex數(shù)據(jù)綁定觀察者在綁定發(fā)生時(shí)激活一個(gè)事件監(jiān)聽器??砂凑障旅娴姆独褂?br/>ChangeWatcher 的watch()即可建立一個(gè)Flex數(shù)據(jù)綁定觀察者:

<?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>

看完上述內(nèi)容,你們掌握ActionScript中怎么定義Flex數(shù)據(jù)綁定的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI