数据模型
基本数据模型:
使用<mx:Model>标签和嵌套的xml结构实现
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundColor="#FFFFFF">
<mx:Model id="contactmodel">
<contact>
<fname>Rich</fname>
<lname>Tretola</lname>
<address>101 Skye St.</address>
<city>Anytown</city>
<state>NY</state>
<zip>11111</zip>
<phone>5165559999</phone>
<email>proflex2@gmail.com</email>
</contact>
</mx:Model>
<mx:Panel title="Contact Info" width="200" height="170">
<mx:Label text="{contactmodel.fname} {contactmodel.lname}" />
<mx:Label text="{contactmodel.address}" />
<mx:Label text="{contactmodel.city} {contactmodel.state}, {contactmodel.zip}" />
<mx:Label text="{contactmodel.phone}" />
<mx:Label text="{contactmodel.email}" />
</mx:Panel>
</mx:Application>
也可将xml文件提取了出来,
Contace.xml
<?xml version="1.0"?>
<contact>
<fname>Rich</fname>
<lname>Tretola</lname>
<address>101 Skye St.</address>
<city>Anytown</city>
<state>NY</state>
<zip>11111</zip>
<phone>5165559999</phone>
<email>proflex2@gmail.com</email>
</contact>
使用<mx:Model>绑定Contact.xml文件
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundColor="#FFFFFF">
<mx:Model source="Contact.xml" id="contactmodel"/>
<mx:Panel title="Contact Info" width="200" height="170">
<mx:Label text="{contactmodel.fname} {contactmodel.lname}" />
<mx:Label text="{contactmodel.address}" />
<mx:Label text="{contactmodel.city} {contactmodel.state}, {contactmodel.zip}" />
<mx:Label text="{contactmodel.phone}" />
<mx:Label text="{contactmodel.email}" />
</mx:Panel>
</mx:Application>
使用<mx:XML >绑定Contact.xml文件
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundColor="#FFFFFF">
<mx:XML source="Contact.xml" id="contactmodel"/>
<mx:Panel title="Contact Info" width="200" height="170">
<mx:Label text="{contactmodel.fname} {contactmodel.lname}" />
<mx:Label text="{contactmodel.address}" />
<mx:Label text="{contactmodel.city} {contactmodel.state}, {contactmodel.zip}" />
<mx:Label text="{contactmodel.phone}" />
<mx:Label text="{contactmodel.email}" />
</mx:Panel>
</mx:Application>
将数据模型绑定到自定义组件
包括公共[Bindable]变量contactmodel(Object类型)的VBOX容器
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Bindable]
public var contactmodel:Object;
]]>
</mx:Script>
<mx:Label text="{contactmodel.fname} {contactmodel.lname}" />
<mx:Label text="{contactmodel.address}" />
<mx:Label text="{contactmodel.city} {contactmodel.state}, {contactmodel.zip}" />
<mx:Label text="{contactmodel.phone}" />
<mx:Label text="{contactmodel.email}" />
</mx:VBox>
使用<mx:Model>和自定义组件绑定Contact.xml文件
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundColor="#FFFFFF" xmlns:comps="*">
<mx:Model source="Contact.xml" id="contactmodel"/>
<mx:Panel title="Contact Info" width="200" height="170">
<comps:Contact contactmodel="{this.contactmodel}" />
</mx:Panel>
</mx:Application>
绑定两个xml文件
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundColor="#FFFFFF" xmlns:comps="*">
<mx:Model source="Contact.xml" id="contactmodel"/>
<mx:Model source="Contact2.xml" id="vacationmodel"/>
<mx:Panel title="Contact Info" width="300" height="170" layout="horizontal">
<comps:Contact contactmodel="{this.contactmodel}" />
<mx:Spacer width="20" />
<comps:Contact contactmodel="{this.vacationmodel}" />
</mx:Panel>
</mx:Application>
posted on 2011-03-16 15:39
长春语林科技 阅读(195)
评论(0) 编辑 收藏 所属分类:
flex