自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Flex數(shù)據(jù)綁定中綁定到函數(shù)、對(duì)象和數(shù)組

開(kāi)發(fā) 后端
Flex數(shù)據(jù)綁定為在應(yīng)用的不同層之間傳遞數(shù)據(jù)提供了便捷方法,本文向大家簡(jiǎn)單介紹一下如何綁定導(dǎo)函數(shù)、對(duì)象和數(shù)組。

本文和大家重點(diǎn)討論一下Flex數(shù)據(jù)綁定中如何綁定到函數(shù)、對(duì)象和數(shù)組,F(xiàn)lex數(shù)據(jù)綁定是將一個(gè)對(duì)象中的數(shù)據(jù)同另一個(gè)對(duì)象中的數(shù)據(jù)聯(lián)系在一起的過(guò)程。

Flex數(shù)據(jù)綁定

1.綁定到函數(shù)、對(duì)象和數(shù)組

(1)綁定函數(shù)以響應(yīng)Flex數(shù)據(jù)綁定事件

可以把使用“不可綁定的參數(shù)”的函數(shù)作為Flex數(shù)據(jù)綁定表達(dá)式的源。但是,必須有一種辦法能夠激活這個(gè)函數(shù)以更新Flex數(shù)據(jù)綁定的目的屬性。
在下面的例子中,使用了[Bindable]元數(shù)據(jù)標(biāo)記來(lái)指定Felx調(diào)用isEnabled()函數(shù)以響應(yīng)myFlagChanged事件。當(dāng)myFlag的setter方法被調(diào)用時(shí),它就發(fā)出了一個(gè)myFlagChanged事件,這個(gè)事件觸發(fā)任何使用isEnabled()函數(shù)作為源的Flex數(shù)據(jù)綁定。
 

  1. <?xmlversionxmlversion="1.0"?> 
  2.  
  3. <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"> 
  4.  
  5. <![CDATA[  
  6. importflash.events.Event;  
  7. //Defineafunctionthatgetsinvoked  
  8. //inresponsetothemyFlagChangedevent.  
  9. [Bindable(event="myFlagChanged")]  
  10. privatefunctionisEnabled():String{  
  11. if(myFlag)  
  12. return'true';  
  13. else  
  14. return'false';  
  15. }  
  16. privatevar_myFlag:Boolean=false;  
  17. //Defineasettermethodthatdispatchesthe  
  18. //myFlagChangedeventtotriggerthedatabinding.  
  19. publicfunctionsetmyFlag(value:Boolean):void{  
  20. _myFlag=value;  
  21. dispatchEvent(newEvent("myFlagChanged"));  
  22. }  
  23. publicfunctiongetmyFlag():Boolean{  
  24. return_myFlag;  
  25. }  
  26. ]]> 
  27.  

#p# (2)將對(duì)象用于Flex數(shù)據(jù)綁定

當(dāng)使用對(duì)象進(jìn)行工作時(shí),不得不考慮什么時(shí)候定義到這個(gè)對(duì)象的綁定?或者考慮什么時(shí)候定義一個(gè)到這個(gè)對(duì)象屬性的綁定?

綁定到對(duì)象

當(dāng)使一個(gè)對(duì)象成為Flex數(shù)據(jù)綁定表達(dá)式的源時(shí),F(xiàn)lex數(shù)據(jù)綁定發(fā)生在這個(gè)對(duì)象被更新之時(shí),或者這個(gè)對(duì)象的引用被更新之時(shí),但不能發(fā)生在這個(gè)對(duì)象的單個(gè)(數(shù)據(jù))域(feild)被更新之時(shí)。
下面的范例中,創(chuàng)建了Object類(lèi)的子類(lèi),這個(gè)子類(lèi)帶有兩個(gè)屬性,stringProp和intProp,但沒(méi)有使這兩個(gè)屬性成為可綁定屬性:
 

  1. packagemyComponents  
  2. {  
  3. //binding/myComponents/NonBindableObject.as  
  4. //Makenoclasspropertiesbindable.  
  5. publicclassNonBindableObjectextendsObject{  
  6. publicfunctionNonBindableObject(){  
  7. super();  
  8. }  
  9. publicvarstringProp:String="Stringproperty";  
  10. publicvarintProp:int=52;  
  11. }  
  12. }  

因?yàn)檫@個(gè)類(lèi)的兩個(gè)屬性不是可綁定屬性,當(dāng)它們被更新時(shí)Flex不會(huì)發(fā)出事件去觸發(fā)Flex數(shù)據(jù)綁定。接下來(lái)在Flex應(yīng)用中使用這個(gè)類(lèi),如下面的范例所示:
 

  1. <?xmlversionxmlversion="1.0"?> 
  2.  
  3. <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml" 
  4. creationComplete="initObj();"> 
  5.  
  6. <![CDATA[  
  7. importmyComponents.NonBindableObject;  
  8. [Bindable]  
  9. publicvarmyObj:NonBindableObject=newNonBindableObject();  
  10. [Bindable]  
  11. publicvaranotherObj:NonBindableObject=  
  12. newNonBindableObject();  
  13. publicfunctioninitObj():void{  
  14. anotherObj.stringProp='anotherObject';  
  15. anotherObj.intProp=8;  
  16. }  
  17. ]]> 
  18.  
  19. <mx:Buttonlabelmx:Buttonlabel="ChangemyObj.stringProp" 
  20. click="myObj.stringProp='newstring';"/> 
  21.  
  22. <mx:Buttonlabelmx:Buttonlabel="ChangemyObj.intProp" 
  23. click="myObj.intProp=10;"/> 
  24.  
  25. <mx:Buttonlabelmx:Buttonlabel="ChangemyObj" 
  26. click="myObj=anotherObj;"/> 
  27.  

 因?yàn)闆](méi)有使NonBindableObject類(lèi)的單個(gè)數(shù)據(jù)域(fields)成為可綁定屬性,所以應(yīng)用在兩個(gè)Text控件的綁定在應(yīng)用啟動(dòng)時(shí)以及在myObj被更新時(shí)才會(huì)被更新。在編譯這個(gè)應(yīng)用時(shí),編譯器會(huì)輸出警告信息,提示Flex數(shù)據(jù)綁定機(jī)制不能檢測(cè)stringProp和intProp屬性的變化。

#p#(3)綁定到對(duì)象的屬性

為了使對(duì)象的屬性可綁定,要?jiǎng)?chuàng)建新的類(lèi)定義,如下面的范例所示:
 

  1. packagemyComponents  
  2. {  
  3. //binding/myComponents/BindableObject.as  
  4. //Makeallclasspropertiesbindable.  
  5. [Bindable]  
  6. publicclassBindableObjectextendsObject{  
  7. publicfunctionBindableObject(){  
  8. super();  
  9. }  
  10. publicvarstringProp:String="Stringproperty";  
  11. publicvarintProp:int=52;  
  12. }  
  13. }  

通過(guò)在類(lèi)定義之前放置[Bindable]元數(shù)據(jù)標(biāo)記,就可以使得類(lèi)中所有public變量、以及所有完全具備setter及getter的public屬性成為可綁定的屬性。接下來(lái)就可以使用stringProp和intProp屬性作為Flex數(shù)據(jù)綁定的源,如下范例所示:

  1. <?xmlversionxmlversion="1.0"?> 
  2.  
  3. <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml" 
  4. creationComplete="initObj();"> 
  5.  
  6. <![CDATA[  
  7. importmyComponents.BindableObject;  
  8. [Bindable]  
  9. publicvarmyObj:BindableObject=newBindableObject();  
  10. [Bindable]  
  11. publicvaranotherObj:BindableObject=  
  12. newBindableObject();  
  13. publicfunctioninitObj():void{  
  14. anotherObj.stringProp='anotherObject';  
  15. anotherObj.intProp=8;  
  16. }  
  17. ]]> 
  18.  
  19. <mx:Buttonlabelmx:Buttonlabel="ChangemyObj.stringProp" 
  20. click="myObj.stringProp='newstring';"/> 
  21.  
  22. <mx:Buttonlabelmx:Buttonlabel="ChangemyObj.intProp" 
  23. click="myObj.intProp=10;"/> 
  24.  
  25. <mx:Buttonlabelmx:Buttonlabel="ChangemyObj" 
  26. click="myObj=anotherObj;"/> 
  27.  

#p# (4)在綁定中使用數(shù)組

在使用數(shù)組進(jìn)行工作時(shí),比如Array或者ArrayCollection對(duì)象,可以把數(shù)組作為Flex數(shù)據(jù)綁定表達(dá)式的源或目的。
注意:當(dāng)使用數(shù)組作為綁定源時(shí),應(yīng)該使用ArrayCollection類(lèi)型的數(shù)組,因?yàn)锳rrayCollection類(lèi)在數(shù)組或數(shù)組元素發(fā)生變化時(shí)能夠發(fā)出事件來(lái)觸發(fā)Flex數(shù)據(jù)綁定。比如,對(duì)ArrayCollection.addItem(),ArrayCollection.addItemAt(),ArrayCollection.removeItem(),以及ArrayCollection.removeItemAt()方法的調(diào)用都會(huì)觸發(fā)Flex數(shù)據(jù)綁定。

綁定到數(shù)組

通常將數(shù)組綁定給Flex控件的dataProvider屬性,下面范例說(shuō)明將數(shù)組綁定用于List控
件:
 

  1. <?xmlversionxmlversion="1.0"?> 
  2.  
  3. <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"> 
  4.  
  5. <![CDATA[  
  6. importmx.collections.ArrayCollection;  
  7. [Bindable]  
  8. publicvarmyAC:ArrayCollection=newArrayCollection([  
  9. "One","Two","Three","Four"]);  
  10. [Bindable]  
  11. publicvarmyAC2:ArrayCollection=newArrayCollection([  
  12. "Uno","Dos","Tres","Quatro"]);  
  13. ]]> 
  14.  
  15. <mx:Button 
  16. label="ChangeElement" 
  17. click="myAC[0]='modOne'"/> 
  18.  
  19. <mx:Button 
  20. label="AddElement" 
  21. click="myAC.addItem('newelement');"/> 
  22.  
  23. <mx:Button 
  24. label="RemoveElement0" 
  25. click="myAC.removeItemAt(0);"/> 
  26.  
  27. <mx:Button 
  28. label="ChangeArrayCollection" 
  29. click="myAC=myAC2"/> 
  30.  

 這個(gè)例子定義了一個(gè)ArrayCollection對(duì)象,然后將List控件的dataProvider屬性設(shè)置為對(duì)這個(gè)ArrayCollection的Flex數(shù)據(jù)綁定。當(dāng)修改ArrayCollection對(duì)象中的元素,或者修改對(duì)ArrayCollection對(duì)象的引用,都會(huì)觸發(fā)Flex數(shù)據(jù)綁定。

綁定到數(shù)組中的元素

可以使用數(shù)組中的單個(gè)元素作為Flex數(shù)據(jù)綁定源,如下例所示:

  1. <?xmlversionxmlversion="1.0"?> 
  2.  
  3. <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"> 
  4.  
  5. <![CDATA[  
  6. importmx.collections.ArrayCollection;  
  7. [Bindable]  
  8. publicvarmyAC:ArrayCollection=newArrayCollection([  
  9. "One","Two","Three","Four"]);  
  10. [Bindable]  
  11. publicvarmyAC2:ArrayCollection=newArrayCollection([  
  12. "Uno","Dos","Tres","Quatro"]);  
  13. ]]> 
  14.  
  15. <mx:Buttonidmx:Buttonid="button1" 
  16. label="ChangeElement" 
  17. click="myAC[0]='newOne'"/> 
  18. <mx:Buttonidmx:Buttonid="button2" 
  19. label="ChangeArrayCollection" 
  20. click="myAC=myAC2"/> 
  21.  

 如果通過(guò)方括號(hào)語(yǔ)法[]來(lái)指定數(shù)組元素作為Flex數(shù)據(jù)綁定表達(dá)式的源,那么Flex數(shù)據(jù)綁定只在應(yīng)用啟動(dòng)時(shí)觸發(fā),或者在數(shù)組或其引用被更新時(shí)觸發(fā)。當(dāng)這個(gè)數(shù)組元素被更新的時(shí)候不會(huì)觸發(fā)Flex數(shù)據(jù)綁定。
但Flex數(shù)據(jù)綁定表達(dá)式中的myAC.getItemAt(0)則會(huì)在該數(shù)組元素變化時(shí)被觸發(fā)更新。因此,id為text2的Text控件在點(diǎn)擊button1時(shí)會(huì)被更新,而id為text1的Text控件則不會(huì)被更新。
當(dāng)使用數(shù)組中的元素作為Flex數(shù)據(jù)綁定表示的源時(shí),應(yīng)當(dāng)在綁定表達(dá)式中使用ArrayCollection.getItemAt()方法。

點(diǎn)擊button2時(shí)將myAC2拷貝給myAC,這會(huì)觸發(fā)對(duì)數(shù)組元素的所有Flex數(shù)據(jù)綁定而不論它們是如何實(shí)現(xiàn)的。

【編輯推薦】

  1. 技術(shù)分享 在ActionScript中如何定義Flex數(shù)據(jù)綁定
  2. Adobe Flex提供三種方法指定Flex數(shù)據(jù)綁定
  3. 術(shù)語(yǔ)匯編 Flex數(shù)據(jù)綁定概述
  4. 解析Flex全屏模式設(shè)置方法
  5. Flex內(nèi)存泄露解決方法和內(nèi)存釋放優(yōu)化原則

 

責(zé)任編輯:佚名 來(lái)源: myspace.com
相關(guān)推薦

2010-07-30 10:53:53

Flex數(shù)據(jù)綁定

2010-07-28 13:31:10

Flex數(shù)據(jù)綁定

2010-07-28 13:11:13

Flex數(shù)據(jù)綁定

2010-08-12 11:34:15

Flex數(shù)據(jù)綁定

2010-07-30 09:08:21

Flex數(shù)據(jù)綁定

2010-08-05 15:06:19

Flex數(shù)據(jù)綁定

2010-07-30 10:58:03

Flex數(shù)據(jù)綁定

2010-08-10 10:56:39

2010-07-28 13:40:44

Flex數(shù)據(jù)綁定

2010-08-12 10:56:17

Flex數(shù)據(jù)綁定

2010-07-28 13:24:20

Flex數(shù)據(jù)綁定

2010-08-11 15:35:47

Flex DataGr

2010-08-11 15:51:45

Flex DataGr

2010-08-12 11:05:33

Flex數(shù)據(jù)綁定

2010-07-30 09:16:24

Flex數(shù)據(jù)綁定

2010-07-28 13:48:49

Flex數(shù)據(jù)綁定

2010-07-30 10:23:46

Flex數(shù)據(jù)綁定

2010-07-30 10:37:23

Flex數(shù)據(jù)綁定

2010-08-13 14:19:44

Flex綁定機(jī)制

2010-08-06 10:15:35

Flex綁定
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)