數(shù)據(jù)綁定是一個(gè)對(duì)象的數(shù)據(jù)綁定到另一個(gè)對(duì)象的過程。 數(shù)據(jù)綁定需要源屬性,目標(biāo)屬性和指示何時(shí)從源到目標(biāo)復(fù)制數(shù)據(jù)的觸發(fā)事件。
Flex提供了三種方式來執(zhí)行數(shù)據(jù)綁定
MXML腳本中的大括號(hào)語法({})
< fx:binding> 標(biāo)簽
ActionScript中的BindingUtils
以下示例演示使用大括號(hào)指定源到目標(biāo)的數(shù)據(jù)綁定。
<s:TextInput id="txtInput1"/> <s:TextInput id="txtInput2" text = "{txtInput1.text}"/>
以下示例演示使用< fx:Binding> 標(biāo)記以指定源到目標(biāo)的數(shù)據(jù)綁定。
<fx:Binding source="txtInput1.text" destination="txtInput2.text" /> <s:TextInput id="txtInput1"/> <s:TextInput id="txtInput2"/>
以下示例演示使用BindingUtils指定源到目標(biāo)的數(shù)據(jù)綁定。
<fx:Script> <![CDATA[ import mx.binding.utils.BindingUtils; import mx.events.FlexEvent; protected function txtInput2_preinitializeHandler(event:FlexEvent):void { BindingUtils.bindProperty(txtInput2,"text",txtInput1, "text"); } ]]> </fx:Script> <s:TextInput id="txtInput1"/> <s:TextInput id="txtInput2" preinitialize="txtInput2_preinitializeHandler(event)"/>
讓我們按照以下步驟在Flex應(yīng)用程序中通過創(chuàng)建測試應(yīng)用程序來查看操作中的換膚:
步 | 描述 |
---|---|
1 | 在 Flex - 創(chuàng)建應(yīng)用程序章節(jié)中所述,在包 com.tutorialspoint.client 下創(chuàng)建名為 HelloWorld 的項(xiàng)目。 |
2 | 修改 HelloWorld.mxml ,如下所述。 保持文件的其余部分不變。 |
3 | 編譯并運(yùn)行應(yīng)用程序,以確保業(yè)務(wù)邏輯按照要求工作。 |
以下是修改的 HelloWorld.mxml 文件 src / com / tutorialspoint / client / HelloWorld.mxml 的內(nèi)容。
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" minWidth="500" minHeight="500" > <fx:Style source="/com/tutorialspoint/client/Style.css"/> <fx:Script> <![CDATA[ import mx.binding.utils.BindingUtils; import mx.events.FlexEvent; protected function txtInput6_preinitializeHandler(event:FlexEvent):void { BindingUtils.bindProperty(txtInput6,"text",txtInput5, "text"); } ]]> </fx:Script> <fx:Binding source="txtInput3.text" destination="txtInput4.text" /> <s:BorderContainer width="500" height="550" id="mainContainer" styleName="container"> <s:VGroup width="100%" height="100%" gap="50" horizontalAlign="center" verticalAlign="middle"> <s:Label id="lblHeader" text="Data Binding Demonstration" fontSize="40" color="0x777777" styleName="heading"/> <s:Panel title="Example #1 (Using Curly Braces,\{\})" width="400" height="100" > <s:layout> <s:VerticalLayout paddingTop="10" paddingLeft="10"/> </s:layout> <s:HGroup > <s:Label text = "Type here: " width="100" paddingTop="6"/> <s:TextInput id="txtInput1"/> </s:HGroup> <s:HGroup > <s:Label text = "Copied text: " width="100" paddingTop="6"/> <s:TextInput id="txtInput2" text = "{txtInput1.text}"/> </s:HGroup> </s:Panel> <s:Panel title="Example #2 (Using <fx:Binding>)" width="400" height="100" > <s:layout> <s:VerticalLayout paddingTop="10" paddingLeft="10"/> </s:layout> <s:HGroup > <s:Label text = "Type here: " width="100" paddingTop="6"/> <s:TextInput id="txtInput3"/> </s:HGroup> <s:HGroup > <s:Label text = "Copied text: " width="100" paddingTop="6"/> <s:Label id="txtInput4"/> </s:HGroup> </s:Panel> <s:Panel title="Example #3 (Using BindingUtils)" width="400" height="100" > <s:layout> <s:VerticalLayout paddingTop="10" paddingLeft="10"/> </s:layout> <s:HGroup > <s:Label text = "Type here: " width="100" paddingTop="6"/> <s:TextInput id="txtInput5"/> </s:HGroup> <s:HGroup > <s:Label text = "Copied text: " width="100" paddingTop="6"/> <s:TextInput enabled="false" id="txtInput6" preinitialize="txtInput6_preinitializeHandler(event)"/> </s:HGroup> </s:Panel> </s:VGroup> </s:BorderContainer> </s:Application>
準(zhǔn)備好所有更改后,讓我們以正常模式編譯和運(yùn)行應(yīng)用程序,就像在 Flex - 創(chuàng)建應(yīng)用程序中一樣 章節(jié)。
如果一切順利,您的應(yīng)用程序,這將產(chǎn)生以下結(jié)果:[在線試用]
更多建議: