课程表

Flex课程

工具箱
速查手册

Flex 数据绑定

当前位置:免费教程 » 软件/图像 » Flex

什么是数据绑定?

数据绑定是一个对象的数据绑定到另一个对象的过程。 数据绑定需要源属性,目标属性和指示何时从源到目标复制数据的触发事件。

Flex提供了三种方式来执行数据绑定

  • MXML脚本中的大括号语法({})

  • < fx:binding> 标签

  • ActionScript中的BindingUtils

数据绑定 - 在MXML中使用花括号

以下示例演示使用大括号指定源到目标的数据绑定。

  1. <s:TextInput id="txtInput1"/>
  2. <s:TextInput id="txtInput2" text = "{txtInput1.text}"/>

数据绑定 - 使用< fx:Binding> 标签

以下示例演示使用< fx:Binding> 标记以指定源到目标的数据绑定。

  1. <fx:Binding source="txtInput1.text" destination="txtInput2.text" />
  2. <s:TextInput id="txtInput1"/>
  3. <s:TextInput id="txtInput2"/>

数据绑定 - 在ActionScript中使用BindingUtils

以下示例演示使用BindingUtils指定源到目标的数据绑定。

  1. <fx:Script>
  2. <![CDATA[
  3. import mx.binding.utils.BindingUtils;
  4. import mx.events.FlexEvent;
  5.  
  6. protected function txtInput2_preinitializeHandler(event:FlexEvent):void
  7. {
  8. BindingUtils.bindProperty(txtInput2,"text",txtInput1, "text");
  9. }
  10. ]]>
  11. </fx:Script>
  12. <s:TextInput id="txtInput1"/>
  13. <s:TextInput id="txtInput2"
  14. preinitialize="txtInput2_preinitializeHandler(event)"/>

Flex数据绑定示例

让我们按照以下步骤在Flex应用程序中通过创建测试应用程序来查看操作中的换肤:

描述
1 Flex - 创建应用程序章节中所述,在包 com.tutorialspoint.client 下创建名为 HelloWorld 的项目。
2修改 HelloWorld.mxml ,如下所述。 保持文件的其余部分不变。
3编译并运行应用程序,以确保业务逻辑按照要求工作。

以下是修改的 HelloWorld.mxml 文件 src / com / tutorialspoint / client / HelloWorld.mxml 的内容。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  3. xmlns:s="library://ns.adobe.com/flex/spark"
  4. xmlns:mx="library://ns.adobe.com/flex/mx"
  5. width="100%" height="100%" minWidth="500" minHeight="500"
  6. >
  7. <fx:Style source="/com/tutorialspoint/client/Style.css"/>
  8. <fx:Script>
  9. <![CDATA[
  10. import mx.binding.utils.BindingUtils;
  11. import mx.events.FlexEvent;
  12.  
  13. protected function txtInput6_preinitializeHandler(event:FlexEvent):void
  14. {
  15. BindingUtils.bindProperty(txtInput6,"text",txtInput5, "text");
  16. }
  17. ]]>
  18. </fx:Script>
  19. <fx:Binding source="txtInput3.text" destination="txtInput4.text" />
  20. <s:BorderContainer width="500" height="550" id="mainContainer"
  21. styleName="container">
  22. <s:VGroup width="100%" height="100%" gap="50" horizontalAlign="center"
  23. verticalAlign="middle">
  24. <s:Label id="lblHeader" text="Data Binding Demonstration"
  25. fontSize="40" color="0x777777" styleName="heading"/>
  26. <s:Panel title="Example #1 (Using Curly Braces,\{\})" width="400"
  27. height="100" >
  28. <s:layout>
  29. <s:VerticalLayout paddingTop="10" paddingLeft="10"/>
  30. </s:layout>
  31. <s:HGroup >
  32. <s:Label text = "Type here: " width="100" paddingTop="6"/>
  33. <s:TextInput id="txtInput1"/>
  34. </s:HGroup>
  35. <s:HGroup >
  36. <s:Label text = "Copied text: " width="100" paddingTop="6"/>
  37. <s:TextInput id="txtInput2" text = "{txtInput1.text}"/>
  38. </s:HGroup>
  39. </s:Panel>
  40. <s:Panel title="Example #2 (Using &lt;fx:Binding&gt;)" width="400"
  41. height="100" >
  42. <s:layout>
  43. <s:VerticalLayout paddingTop="10" paddingLeft="10"/>
  44. </s:layout>
  45. <s:HGroup >
  46. <s:Label text = "Type here: " width="100" paddingTop="6"/>
  47. <s:TextInput id="txtInput3"/>
  48. </s:HGroup>
  49. <s:HGroup >
  50. <s:Label text = "Copied text: " width="100" paddingTop="6"/>
  51. <s:Label id="txtInput4"/>
  52. </s:HGroup>
  53. </s:Panel>
  54. <s:Panel title="Example #3 (Using BindingUtils)" width="400"
  55. height="100" > <s:layout>
  56. <s:VerticalLayout paddingTop="10" paddingLeft="10"/>
  57. </s:layout>
  58. <s:HGroup >
  59. <s:Label text = "Type here: " width="100" paddingTop="6"/>
  60. <s:TextInput id="txtInput5"/>
  61. </s:HGroup>
  62. <s:HGroup >
  63. <s:Label text = "Copied text: " width="100" paddingTop="6"/>
  64. <s:TextInput enabled="false" id="txtInput6"
  65. preinitialize="txtInput6_preinitializeHandler(event)"/>
  66. </s:HGroup>
  67. </s:Panel>
  68. </s:VGroup>
  69. </s:BorderContainer>
  70. </s:Application>

准备好所有更改后,让我们以正常模式编译和运行应用程序,就像在 Flex - 创建应用程序中一样 章节。
如果一切顺利,您的应用程序,这将产生以下结果:[在线试用]

Flex Data Binding
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号