﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-黑灵客栈-随笔分类-RIA</title><link>http://www.blogjava.net/mstar/category/1720.html</link><description>搞软件开发就像被强奸,如果不能反抗,就享受它吧！</description><language>zh-cn</language><lastBuildDate>Sat, 02 Jun 2007 03:13:06 GMT</lastBuildDate><pubDate>Sat, 02 Jun 2007 03:13:06 GMT</pubDate><ttl>60</ttl><item><title>Flex编程模型--转贴</title><link>http://www.blogjava.net/mstar/archive/2007/05/31/121154.html</link><dc:creator>黑灵</dc:creator><author>黑灵</author><pubDate>Thu, 31 May 2007 06:16:00 GMT</pubDate><guid>http://www.blogjava.net/mstar/archive/2007/05/31/121154.html</guid><wfw:comment>http://www.blogjava.net/mstar/comments/121154.html</wfw:comment><comments>http://www.blogjava.net/mstar/archive/2007/05/31/121154.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mstar/comments/commentRss/121154.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mstar/services/trackbacks/121154.html</trackback:ping><description><![CDATA[
		<div id="art" style="margin: 15px;">
				<div>转贴至 http://blog.csdn.net/mylxiaoyi/archive/2007/05/16/1611579.aspx<br />Flex包含Flex类库，MXML以及ActionScript程序语言，如下表所示：</div>
				<img src="http://blog.chinaunix.net/photo/19185_070512091659.png" alt="" />
				<div>
						<br />包含在Flex中，但是却并没有在上图中显示的还有Flex编译以及Flex调试器。<br />我们使用MXML与ActionScript的组合来编写Flex程序。MXML和ActionScript程序语言都给了我们访问Flex类库的能力。使用MXML来定义程序的用户界面元素，使用ActionScript来定义客户逻辑与过程控制。<br />Flex类库包含Flex组件，管理器与行为。使用基于组件的Flex开发模型，开发者可以合并预编译的组件，创建新的组件，或者是组合预编译的组件为复合组件。</div>
				<div>
						<br />
						<strong>Flex类库与MXML和ActionScript的关系</strong>
						<br />Flex是作为ActionScript类库来实现的。这个类库包含组件（容器与控件），管理类，数据服务类以及其他特性的类。我们使用MXML和ActionScript语言配合类库来开发程序。<br />MXML
标签与ActionScript类或是类的属性相对应。Flex分析MXML标签，并且编译包含相应ActionScript对象的SWF文件。例如，
Flex提供了ActionScript Button类来定义Flex按钮控件。在MXML中，我们可以用下面的MXML语句来创建一个按钮控件：<br />&lt;mx:Button label="Submit"/&gt;<br />当我们使用MXML标签声明了一个控件，我们就创建了那个类的一个实例。这个MXML语句创建了一个按钮对象，并且初始化这个按钮对象的label属性为Submit字符串。<br />与ActionScript
类相对应的MXML使用与ActionScript类相同的命名约定。类名以一个大写字母开头，而且在类名中以大写字母分隔单词。每一个MXML标签属性
对应着ActionScript对象的一个属性，适合于对象的一种风格，或者是这个对象的一个事件监听器。</div>
				<div>
						<br />
						<strong>布局我们的程序</strong>
						<br />我们使用代表用户界面的组件来声明Flex程序的用户界面。有两种类型的组件：控件与容器。控件是窗体元素，如按钮，文本框，列表框。容器是屏幕上包含控件和其他容器的矩形区域。<br />在Flex的根部是单一的容器，称之为程序容器，这代表整个Flash播放器的界面。这个程序容器保存所有的其他的代表对话框，面板和窗体的容器。<br />一个容器有预定义的规则来布局他的孩子容器和控件，包括尺寸以及位置。Flex定义了布局规则来简化富网络程序的设计与实现，然而也提供了足够的灵活性来使得我们可以创建多样的程序集合。<br />使用预定义的布局规则的一个优点就是我们的用户可以很快的习惯我们的程序。也就是说，通过标准化用户交互的规则，我们的用户就不需要考虑如何来导航程序，相反却可以集中于程序所提供的内容。<br />另
一个优点就是我们不必在定义导航以及作为设计一部分的布局规则上花费大量的时间。相反，我们可以集中精力于我们想要传递的信息以及我们要提供给用户的选
择，并且不需为所有用户行为的细节与程序响应担心。通过这样的方式，Flex提供了一个结构可以让我们快速容易的使用丰富的特性与交互集合来开发程序。</div>
				<div>
						<br />
						<strong>使用Flex Builder来布局我们的程序</strong>
						<br />我
们可以使用Flex Builder构建Flex程序的用户界面。我们可以使用Flex
Builder通过可视的编码与设计工具来开发Flex与ActionScript程序。我们也可以使用可以提供代码提示与代码辅助特性的编辑编写
MXML，ActionScript与CSS代码，从而帮助我们的开发工作。<br />在这个编辑器中，我们可以切换到设计模式来可视化的开发我们的程序，在设计画布上使用容器与组件，使用约束来布局我们的组件，并查看运行时用户界面的转化。然后我们使用定制的编译器来构建我们的工程，并且使用集成的调试工具来调试我们的工程。</div>
				<div>
						<br />
						<strong>在MXML中定义用户界面</strong>
						<br />我们的程序可以由一个或是多个MXML文件组成。使用多个MXML文件可以促进代码重用，简化构建复杂程序的过程，并且使得多个开发者贡献同一个工程变得更为容易。<br />下面的例子是一个MXML程序，这个程序使用一个按钮控件来触发从一个文本输入控件拷贝文本到一个文本区控件：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">&lt;?</span><span style="color: rgb(255, 0, 255);">xml version="1.0" encoding="utf-8"</span><span style="color: rgb(0, 0, 255);">?&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> ?xml tag must start in line 1 column 1 </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> MXML root element tag. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Application </span><span style="color: rgb(255, 0, 0);">xmlns:mx</span><span style="color: rgb(0, 0, 255);">="http://www.adobe.com/2006/mxml"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Flex controls exist in a container. Define a Panel container. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Panel </span><span style="color: rgb(255, 0, 0);">title</span><span style="color: rgb(0, 0, 255);">="My Application"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> TextInput control for user input. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:TextInput </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myInput"</span><span style="color: rgb(255, 0, 0);"> width</span><span style="color: rgb(0, 0, 255);">="150"</span><span style="color: rgb(255, 0, 0);"> text</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Output TextArea control. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:TextArea </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myText"</span><span style="color: rgb(255, 0, 0);"> text</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(255, 0, 0);"> width</span><span style="color: rgb(0, 0, 255);">="150"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Button control that triggers the copy. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Button </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myButton"</span><span style="color: rgb(255, 0, 0);"> label</span><span style="color: rgb(0, 0, 255);">="Copy Text"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />           <br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">mx:Panel</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">mx:Application</span><span style="color: rgb(0, 0, 255);">&gt;</span></div><br /></div>
				<div>
						<br />这个程序第一行指明了XML声明，而且必须放在MXML文件的第一行，第一列。<br />第二行以&lt;mx:Application&gt;标签开始，这是Flex程序的根元素。这个标签包含了Flex名字空间声明。在起始与结束的&lt;mx:Application&gt;标签之间的部分定义了Flex程序。<br />下图演示了在Flash播放器中运行这个程序的效果图：</div>
				<img src="http://blog.chinaunix.net/photo/19185_070512091717.png" alt="" />
				<div>
						<br />正如这段代码所写的，这个例子布局了我们的用户界面，但是却并没有包含从文本输入框控件到文本区控件的拷贝逻辑。</div>
				<div>
						<br />
						<strong>向Flex程序中添加ActionScript</strong>
						<br />我们可以为了下列目的使用ActionScript<br />处理事件 Flex用户界面是事件驱动的。例如，当用户选择一个按钮时，这个按钮生成一个事件。我们在ActionScript中定义名为事件监听器的函数来处理事件。我们的事件监听器可以打开一个窗口，播放SWF文件，或者是执行我们的程序所必须的任何动作。<br />处理错误 我们在ActionScript处理运行时错误。我们可以检测数据验证错误，向用户发送错误信息，向服务器重新提交请求，或者是执行基于我们程序的其他事件。<br />在MXML语句中向Flex控件绑定数据对象 我们可以使用数据绑定从一个Flex控件组织数据模型，或是从数据模型组织组件，或者是从一个组件向另一个组件拷贝数据。<br />定义自定义组件 我们可以从Flex组件类层次继承来创建适合我们程序需求的组件。<br />下面的例子是在前一个例子的基础上所做的更改，为按钮的click事件添加事件监听器。一个事件监听器为了响应用户的事件而执行的ActionScript代码。在我们这个例子中的事件监听器是当用户选择按钮控件时从文本输入控件向文本区拷贝文本：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">&lt;?</span><span style="color: rgb(255, 0, 255);">xml version="1.0" encoding="utf-8"</span><span style="color: rgb(0, 0, 255);">?&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> ?xml tag must start in line 1 column 1 </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> MXML root element tag. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Application </span><span style="color: rgb(255, 0, 0);">xmlns:mx</span><span style="color: rgb(0, 0, 255);">="http://www.adobe.com/2006/mxml"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Flex controls exist in a container. Define a Panel container. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Panel </span><span style="color: rgb(255, 0, 0);">title</span><span style="color: rgb(0, 0, 255);">="My Application"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> TextInput control for user input. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:TextInput </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myInput"</span><span style="color: rgb(255, 0, 0);"> width</span><span style="color: rgb(0, 0, 255);">="150"</span><span style="color: rgb(255, 0, 0);"> text</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Output TextArea control. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:TextArea </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myText"</span><span style="color: rgb(255, 0, 0);"> text</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(255, 0, 0);"> width</span><span style="color: rgb(0, 0, 255);">="150"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Button control that triggers the copy. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Button </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myButton"</span><span style="color: rgb(255, 0, 0);"> label</span><span style="color: rgb(0, 0, 255);">="Copy Text"</span><span style="color: rgb(255, 0, 0);"><br />            click</span><span style="color: rgb(0, 0, 255);">="myText.text=myInput.text;"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />           <br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">mx:Panel</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">mx:Application</span><span style="color: rgb(0, 0, 255);">&gt;</span></div><br /></div>
				<div>前
一个例子在MXML代码中直接插入了ActionScript代码。虽然这对于一或是两行的ActionScript代码可以工作的很好，但是对于更为复
杂的逻辑，通常我们会在&lt;mx:Script&gt;块中定义我们的ActionScript，如下面的例子所示：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">&lt;?</span><span style="color: rgb(255, 0, 255);">xml version="1.0" encoding="utf-8"</span><span style="color: rgb(0, 0, 255);">?&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> ?xml tag must start in line 1 column 1 </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> MXML root element tag </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Application </span><span style="color: rgb(255, 0, 0);">xmlns:mx</span><span style="color: rgb(0, 0, 255);">="http://www.adobe.com/2006/mxml"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Script</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;![CDATA[</span><span style="color: rgb(128, 128, 128);"><br />           <br />            // Define an ActionScript function.<br />            private function duplicate():void {<br />                myText.text=myInput.text;<br />            }<br />        </span><span style="color: rgb(0, 0, 255);">]]&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">mx:Script</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Flex controls exist in a container. Define a Panel container. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Panel </span><span style="color: rgb(255, 0, 0);">title</span><span style="color: rgb(0, 0, 255);">="My Application"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> TextInput control for user input. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:TextInput </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myInput"</span><span style="color: rgb(255, 0, 0);"> width</span><span style="color: rgb(0, 0, 255);">="150"</span><span style="color: rgb(255, 0, 0);"> text</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Output TextArea control. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:TextArea </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myText"</span><span style="color: rgb(255, 0, 0);"> text</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(255, 0, 0);"> width</span><span style="color: rgb(0, 0, 255);">="150"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Button control that triggers the copy. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Button </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myButton"</span><span style="color: rgb(255, 0, 0);"> label</span><span style="color: rgb(0, 0, 255);">="Copy Text"</span><span style="color: rgb(255, 0, 0);"><br />            click</span><span style="color: rgb(0, 0, 255);">="duplicate();"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />           <br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">mx:Panel</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">mx:Application</span><span style="color: rgb(0, 0, 255);">&gt;</span></div><br /></div>
				<div>在
这个例子中，我们使事件监听器作为一个ActionScript函数来实现。Flex为了响应用户选择按钮控件调用这个函数。这种技术使得我们可以将我们
的MXML代码从我们的ActionScript代码中分离出来。为了增加程序的模块性我们也可以选择将我们的程序分为多个文件。</div>
				<div>
						<br />
						<strong>使用数据绑定</strong>
						<br />Flex为了组件之间属性的绑定，或是到数据模型的绑定提供了简单的语法。在下面这个例子中，花括号中的值将文本区的text属性绑定到了文本输入控件的text属性。当用户在文本输入控件中输入文本时，他会自动的拷贝到文本区控件，如下面的例子所示：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">&lt;?</span><span style="color: rgb(255, 0, 255);">xml version="1.0" encoding="utf-8"</span><span style="color: rgb(0, 0, 255);">?&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> ?xml tag must start in line 1 column 1 </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> MXML root element tag. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Application </span><span style="color: rgb(255, 0, 0);">xmlns:mx</span><span style="color: rgb(0, 0, 255);">="http://www.adobe.com/2006/mxml"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Flex controls exist in a container. Define a Panel container. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:Panel </span><span style="color: rgb(255, 0, 0);">title</span><span style="color: rgb(0, 0, 255);">="My Application"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> TextInput control for user input. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:TextInput </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myInput"</span><span style="color: rgb(255, 0, 0);"> width</span><span style="color: rgb(0, 0, 255);">="150"</span><span style="color: rgb(255, 0, 0);"> text</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 128, 0);">&lt;!--</span><span style="color: rgb(0, 128, 0);"> Output TextArea control. </span><span style="color: rgb(0, 128, 0);">--&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">mx:TextArea </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="myText"</span><span style="color: rgb(255, 0, 0);"> text</span><span style="color: rgb(0, 0, 255);">="{myInput.text}"</span><span style="color: rgb(255, 0, 0);"> width</span><span style="color: rgb(0, 0, 255);">="150"</span><span style="color: rgb(0, 0, 255);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">mx:Panel</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">mx:Application</span><span style="color: rgb(0, 0, 255);">&gt;</span></div><br /></div>
				<div> </div>
				<div>
						<strong>控制程序显示</strong>
						<br />Flex定义我们在程序中可以使用的默认显示，或者是修改来定义我们特定的显示。作为修改演示的一部分，我们可以修改下面的一些或是全部：<br /><strong>尺寸</strong> 一个组件或是程序的高度或是宽度。所有的组件都有一个默认的尺寸。我们可以使用默认的尺寸，指定我们自己的尺寸，或者作为布局我们程序的一部分让Flex重新调整一个组件的尺寸。<br /><strong>格式</strong> 特性集合，例如字体，字号，文本对齐属性，颜色。这与在层叠样式表(CSS)定义与使用的相同。<br /><strong>皮肤</strong> 控制组合显示的符号。皮肤化就是通过修改或是替换他的可视化元素来改变一个组件显示的过程。这些元素可以图像，SWF文件，或者是包含绘画API方法的类文件组成。<br /><strong>行为</strong> 程序或是用户行为引起的对于一个Flex组件的可见的或是可听的变化。行为的例子如基于鼠标移动点击的移动或是组件尺寸的重新调整。<br /><strong>视图状态</strong> 在许多富网络程序中，界面的改变基于用户所执行的任务。视图状态可以让我们通过修改基内容来变化一个组件的内容和显示或者是程序。<br /><strong>转化</strong> 转化定义了视图状态的改变如何显示在屏幕上。我们使用特效类，结合处理转化的显式效果设计来定义转化。</div>
				<div>
						<br />
						<strong>使用数据服务工作</strong>
						<br />Flex
被设计用来与可以访问本地和远程服务端逻辑的服务进行交互。例如，一个Flex程序可以使用AMF来连接使用简单对象访问协议(SOAP)的网络服务，返
回XML的HTTP
URL，或者是Flex数据服务，与位于与Flex同一个程序服务器上的Java对象。MXML提供数据访问的组件称之为数据服务组件。MXML包含下列
的一些数据服务组件：<br /><strong>WebService</strong> 提供到基于SOAP的网络服务的访问。<br /><strong>HTTPService</strong> 提供到返回数据的HTTP URL的访问。<br /><strong>RemoteObject</strong> 提供到使用AMF协议的Java对象的访问。这个选项只在Flex数据服务或是Macromedia ColdFusion MX 7.0.2中可用。<br />在
我们的Flex程序中选择如何来访问数据影响着我们程序的性能。因为Flex程序是在第一次请求之后缓存在浏览器中的，当程序运行时，数据访问是影响程序
性能的主要原因。为了向用户传递数据Flex提供了几种解决方案。他将数据交给调用在Flex类路径中装入的Java类的运行时服务，或者是向网络服务或
是HTTP服务器发送代理请求。<br />使用WebService组件允许我们使用基于SOAP的方法，但是他并不总是产生最优的性能。同时，使用SOAP编码的额处XML需要比AMF所做的更多的内容。<br />网络服务中的SOAP性能同时依赖于我们的网络服务的实现。不同的程序服务器使用不同的网络服务后端，所以我们会看到依赖于不同实现的性能。理解我们的实现表现如何的唯一方法就是装入并测试我们的服务。<br />在许多情况下，我们的选择依赖于我们已存在的程序以及我们如何选择将他们集成到后台服务器资源中。网络服务的表现在很大程度上依赖于我们的程序服务器的网络服务引擎的底层实现，所以我们应装入并测试他们的性能。</div>
				<div>
						<br />
						<strong>将我们数据模型从我们的视图中分离</strong>
						<br />为了清楚的分离用户界面，程序相关的数据，数据服务，我们可以使用Flex数据模型来存储控制器与数据服务之间的数据。这种三层设计是输入数据与数据服务共同作用的结果。<br />当我们计划一个程序，我们要决定程序必须存储的数据类型以及这些数据要如何处理。这有助于我们决定我们需要什么类型的数据模型。例如，假设我们决定我们的程序必须存储顾员的相关数据。一个简单的顾员模型可能包含姓名，部门以及邮箱地址属性。<br />一
个Flex数据模型是一个包含我们用来存储程序相关数据的属性的ActionScript对象。我们可以将数据模型用于数据验证，从而他可以包含客户端业
务逻辑。我们可以在MXML或是ActionScript中定义一个数据模型。在MVC设计模式中，数据模型代表模型层。<br />我们可以在MXML标
签，ActionScript函数或是ActionScript类中定义数据模型。在MXML中编写的模型用利于快速开发与简化数据存储，但是他不能提供
任何额外的功能，而我们不可以设定模型属性的数据类型。如果我们想要设定数据类型以及为额外的功能提供方法，我们应使用基于ActionScript的
类。在通常情况下，我们应为了简化数据结构使用基于MXML的模型，为更为复杂的结构以及客户端业务逻辑使用ActionScript。</div>
				<div> </div>
		</div>
<img src ="http://www.blogjava.net/mstar/aggbug/121154.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mstar/" target="_blank">黑灵</a> 2007-05-31 14:16 <a href="http://www.blogjava.net/mstar/archive/2007/05/31/121154.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Flex+Webservice中文乱码问题解决</title><link>http://www.blogjava.net/mstar/archive/2005/06/18/6341.html</link><dc:creator>黑灵</dc:creator><author>黑灵</author><pubDate>Sat, 18 Jun 2005 14:34:00 GMT</pubDate><guid>http://www.blogjava.net/mstar/archive/2005/06/18/6341.html</guid><wfw:comment>http://www.blogjava.net/mstar/comments/6341.html</wfw:comment><comments>http://www.blogjava.net/mstar/archive/2005/06/18/6341.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mstar/comments/commentRss/6341.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mstar/services/trackbacks/6341.html</trackback:ping><description><![CDATA[今天用做的东西要用Flex调用Webserice.没想到出来的中文全是乱码.<BR>于是上网找资料.结果没有找到flex+webservice的，其他的如flex+rometeobject等等的倒是有。但是很多最终也没有说出合适的解决办法。很多人说要把encoding="utf-8"改成GBK、GB2312等等，但是都不奏效，相反原来直接写在component上的中文却成的乱码。于是我觉得，问题不是出在客户端，应该是服务端。于是我想起以前一些解决java中文乱码的问题。用经典的转换方法：<BR>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">这是一个将西欧字符转换为gb2312字符的转换方法</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_79_239_Open_Image onclick="this.style.display='none'; Codehighlighter1_79_239_Open_Text.style.display='none'; Codehighlighter1_79_239_Closed_Image.style.display='inline'; Codehighlighter1_79_239_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_79_239_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_79_239_Closed_Text.style.display='none'; Codehighlighter1_79_239_Open_Image.style.display='inline'; Codehighlighter1_79_239_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;final&nbsp;String&nbsp;getGBString(String&nbsp;src)&nbsp;</SPAN><SPAN id=Codehighlighter1_79_239_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_79_239_Open_Text><SPAN style="COLOR: #000000">{&nbsp;<BR><IMG id=Codehighlighter1_90_161_Open_Image onclick="this.style.display='none'; Codehighlighter1_90_161_Open_Text.style.display='none'; Codehighlighter1_90_161_Closed_Image.style.display='inline'; Codehighlighter1_90_161_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_90_161_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_90_161_Closed_Text.style.display='none'; Codehighlighter1_90_161_Open_Image.style.display='inline'; Codehighlighter1_90_161_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_90_161_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_90_161_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;String(src.getBytes(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">ISO-8859-1</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">),&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">gb2312</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG id=Codehighlighter1_210_237_Open_Image onclick="this.style.display='none'; Codehighlighter1_210_237_Open_Text.style.display='none'; Codehighlighter1_210_237_Closed_Image.style.display='inline'; Codehighlighter1_210_237_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_210_237_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_210_237_Closed_Text.style.display='none'; Codehighlighter1_210_237_Open_Image.style.display='inline'; Codehighlighter1_210_237_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(java.io.UnsupportedEncodingException&nbsp;e)&nbsp;</SPAN><SPAN id=Codehighlighter1_210_237_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_210_237_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV></DIV><BR>在webservice的java类里面的所有中文返回以前都用这个函数转换一下。<BR>OK，问题解决了。<BR>但是并没有完全解决，因为你在所有返回中文的函数里都要有这个方法。<BR>我不知道Webservice的引擎有没有类是Servlet的Filter的东西。以前在解决网页上中文乱码的时候只要写一个Filter类在转换就行了。<img src ="http://www.blogjava.net/mstar/aggbug/6341.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mstar/" target="_blank">黑灵</a> 2005-06-18 22:34 <a href="http://www.blogjava.net/mstar/archive/2005/06/18/6341.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Using Flex Builder to Create Web Service–Based Flex Applications</title><link>http://www.blogjava.net/mstar/archive/2005/06/12/5990.html</link><dc:creator>黑灵</dc:creator><author>黑灵</author><pubDate>Sun, 12 Jun 2005 14:31:00 GMT</pubDate><guid>http://www.blogjava.net/mstar/archive/2005/06/12/5990.html</guid><wfw:comment>http://www.blogjava.net/mstar/comments/5990.html</wfw:comment><comments>http://www.blogjava.net/mstar/archive/2005/06/12/5990.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mstar/comments/commentRss/5990.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mstar/services/trackbacks/5990.html</trackback:ping><description><![CDATA[<!--StartFragment -->&nbsp; 
<H3>Adding Web Services</H3>
<P>Flex Builder makes it easy to add named and unnamed web services to an application by exposing the Flex whitelist.</P>
<OL>
<LI>Open the Data panel by selecting Window &gt; Data (Ctrl + Shift + F7). </LI>
<LI>To add a service, click the Plus (+) button in the Data panel and choose Web Service. </LI>
<LI>
<P>In the pop-up menu, see all of the named and unnamed web services that have been configured in the Flex whitelist (Figure 1).</P><IMG class=image-border height=112 alt="The Data panel." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig01.gif" width=294> 
<P class=caption><STRONG>Figure 1.</STRONG> The Data panel.</P>
<P>The named service, <CODE>SampleEmployeeWS</CODE>, is one of the services displayed in the list. Named web services allow you to reference web services with simple names instead of using web service WSDL URLs. Entries in the flex-config.xml file map named web services to the web service details, such as the WSDL URL and the type of authentication. This means that web service references in your applications aren’t hard coded. You can also set up named services for HTTP services and Remote Objects.</P></LI>
<LI>Select the named service <CODE>SampleEmployeeWS</CODE> from the Add WebService pop-up menu (Figure 2).</LI></OL><IMG class=image-border height=134 alt="The Add WebService dialog box." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig02.gif" width=460> 
<P class=caption><STRONG>Figure 2.</STRONG> The Add WebService dialog box.</P>
<P class=note><STRONG>Note:</STRONG> In the pop-up menu, you might see entries that contain { } or * syntax. These indicate global filters. Consider these indicators as acceptable URL formats. For example <KBD>http://{localserver}/*</KBD> indicates any web service running on the local server, and is legal. For more information on whitelists, see <A href="http://www.macromedia.com/support/flex/ts/documents/whitelist.htm">Configuring the whitelist for Flex applications</A> (TechNote 19251).</P>
<P>Once you have added the service to the application, the web service operations, operation parameters, and operation result objects display in the Data panel (Figure 3). </P><IMG class=image-border height=122 alt="The Data panel with a web service." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig03.gif" width=294> 
<P class=caption><STRONG>Figure 3.</STRONG> The Data panel with a web service.</P>
<P>In the next sections you learn how to:</P>
<UL>
<LI>Create a binding to the web service operation parameters</LI>
<LI>Create a binding from the web service operation result to a component</LI>
<LI>Create an event that invokes the web service operation </LI></UL>
<P><!--StartFragment --></P>
<H3>Binding a Component to a Web Service Request</H3>
<P>Use the following steps to bind the dept ComboBox to the getList operation’s parameter <CODE>deptId</CODE>.</P>
<OL>
<LI>Select the pre-populated ComboBox in Design view. </LI>
<LI>Open the Tag Inspector by selecting Window &gt; Tag Inspector (F9) and selecting the Bindings tab.</LI>
<LI>
<P>Click the Plus (+) button. This displays the Add Binding - Step 1 dialog box.</P>
<P>In the Add Binding dialog box, you must select the binding direction and a property to bind to. The binding direction specifies how data will flow. Data can flow FROM the selected component (an <CODE>out</CODE> binding), or TO the selected component (an <CODE>in</CODE> binding). When you select a direction radio button, the default property for the direction displays in the selected component properties list. The selected component properties are a list of properties that are bindable for the selected component. The list of component properties changes depending upon the binding direction. When the direction is TO (or <CODE>in</CODE>), the write and read/write properties of the selected component display. When the direction is FROM (or <CODE>out</CODE>), the read and read/write properties of the selected component display.</P>
<P>For this binding, data needs to flow from a ComboBox property to the web service operation’s argument. Select the radio button to the left of “Data will flow FROM ‘dept’". The <CODE>selectedItem</CODE> is the default property of the ComboBox, so it is selected by default in the list of properties; it also displays in the Binding Source field at the bottom of the dialog box. The <CODE>selectedItem</CODE> property of the ComboBox is an object composed of a data and a label. In this case, you must bind to the data field. Type <STRONG><CODE>.data</CODE></STRONG> at the end of the <CODE>selectedItem</CODE> property in the Binding source text field so that it is: <STRONG><CODE>selectedItem.data</CODE></STRONG>. Click Next.</P><IMG class=image-border height=392 alt="The Add Binding – Step 1 dialog box with FROM selected." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig04.gif" width=460> 
<P class=caption><STRONG>Figure 4.</STRONG> The Add Binding – Step 1 dialog box with FROM selected.</P></LI>
<LI>
<P>The second bindings dialog box appears. This dialog box contains a list of components that are in the MXML application and a list of component properties that can be bound TO or FROM. </P>
<P>If you select FROM in the initial binding dialog box, the binding is TO the component property you select in the secondary dialog box. Data will flow from the component property you selected in the initial dialog to the component property you select in the second bindings dialog box.</P>
<P>Conversely, if you select TO in the initial binding dialog box, then the binding is FROM the component property you select in the secondary dialog box. Data will flow to the component property you selected in the initial dialog box from the component property you select in the second binding's dialog box.</P>
<P>When you select TO (or <CODE>in</CODE>) in the initial binding dialog box, the secondary dialog box’s component property list displays read and read/write properties. When the direction is FROM or <CODE>out</CODE> in the initial binding dialog box, the secondary dialog box’s component property list displays write and read/write properties.</P>
<P>For this binding, data needs to flow to the web service operation’s deptId argument. Select the web service <CODE>service1</CODE> in the “Data will flow TO “ tree on the left.</P></LI>
<LI>
<P>Expand the <CODE>getList</CODE> operation and its request object in the “Selected component property” tree and select the <CODE>deptId</CODE> argument. The Binding destination displays <CODE>service1.getList.request.deptId</CODE>. </P><IMG class=image-border height=392 alt="The Add Binding – Step 2 dialog displaying TO." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig05.gif" width=460> 
<P class=caption><STRONG>Figure 5.</STRONG> The Add Binding – Step 2 dialog box displaying TO.</P></LI>
<LI>
<P>Click Finish. This creates the binding between the ComboBox and the web service operation argument. Look in Code view. See the generated code:</P>
<DIV class=preWrapper><PRE> &lt;mx:WebService serviceName="SampleEmployeeWS" id="service1"&gt;
      &lt;mx:operation name="getList"&gt;
        &lt;mx:request&gt;
          &lt;deptId&gt;{dept.selectedItem.data}&lt;/deptId&gt;
        &lt;/mx:request&gt;
      &lt;/mx:operation&gt;
&lt;/mx:WebService&gt;
</PRE></DIV>
<P>In Code or Design view, when you select a component with a binding, the component icon, name (or type), and bindings display in the Bindings panel. Selecting the binding in the Bindings panel causes binding direction to display in the Direction field and the component property that the binding is TO or FROM to display the Bound to field. Double-click the binding to edit the full binding. Clicking the Bound to field makes the second binding dialog box appear so that you can edit it.</P>
<P>When you select the ComboBox in Code or Design view, the <CODE>selectedItem.data</CODE> binding displays in the Bindings panel. Selecting the binding displays the direction as <CODE>out</CODE> and the Bound to field displays <CODE>service1.getList.request.deptId</CODE>. When you select the web service, <CODE>service1</CODE>, in the Data panel, <CODE>getList.request.deptId</CODE> displays in the Bindings panel. Selecting the binding displays the direction as <CODE>in</CODE> and the Bound to field displays <CODE>dept.selectedItem.data</CODE>. </P><IMG class=image-border height=216 alt="The Bindings panel when you select the ComboBox" src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig06.gif" width=301> 
<P class=caption><STRONG>Figure 6.</STRONG> The Bindings panel when you select the ComboBox binding </P><IMG class=image-border height=217 alt="The Bindings panel when you select the web service" src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig07.gif" width=301> 
<P class=caption><STRONG>Figure 7.</STRONG> The Bindings panel when you select the web service binding </P></LI></OL>
<P class=caption><!--StartFragment --></P>
<H3>Binding a Component To a Web Service Result</H3>
<P>To display the result of your web service operation call, you must create a binding from the web service result to a component property. This is a similar process to the previous binding, but the direction is opposite. The data needs to flow to the component property from the web service operation result instead of from the component property to the web service operation request.</P>
<OL>
<LI>Select the DataGrid component in Design view and click the Plus (+) button in the Bindings panel. This displays the Add Binding - Step 1 dialog box. </LI>
<LI>The web service result data needs to flow to the DataGrid. Select the "Data will flow TO &lt;mx:DataGrid&gt;" radio button if it hasn't been selected. The default property, dataProvider, is selected in the Component property list. </LI>
<LI>Notice that the binding syntax displays in the Binding Destination field at the bottom of the dialog box as <CODE>dataProvider</CODE>.</LI>
<LI>
<P>Click Next to go to the second binding dialog box.</P><IMG class=image-border height=397 alt="The Add Binding – Step 1 dialog with TO selected" src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig08.gif" width=460> 
<P class=caption><STRONG>Figure 8.</STRONG> The Add Binding – Step 1 dialog with TO selected.</P></LI>
<LI>In the second bindings dialog box, select the web service <CODE>service1</CODE> in the “Data will flow FROM…” tree. </LI>
<LI>Expand the <CODE>getList</CODE> operation in the selected component property tree. </LI>
<LI>
<P>Since the DataGrid <CODE>dataProvider</CODE> property expects an array, select the <CODE>result:(Array)</CODE> node under the <CODE>getList</CODE> operation. The Bindings source at the bottom of the dialog box displays <CODE>service1.getList.result</CODE>. </P><IMG class=image-border height=397 alt="The Add Binding – Step 2 dialog displaying FROM." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig09.gif" width=460> 
<P class=caption><STRONG>Figure 9.</STRONG> The Add Binding – Step 2 dialog displaying FROM.</P></LI>
<LI>Click Finish. The Binding icon now appears in Design view</LI>
<LI>
<P>Go to Code view. Notice the added binding in the DataGrid’s <CODE>dataProvider</CODE> property:</P>
<DIV class=preWrapper><PRE>&lt;mx:DataGrid widthFlex="1" dataProvider="{service1.getList.result}"&gt;
</PRE></DIV></LI></OL><!-- InstanceEndEditable -->
<DIV class=nav-pagination><!--StartFragment -->&nbsp;</DIV>
<H3 class=nav-pagination>Invoking a Web Service from Component Events</H3>
<P class=nav-pagination>Now that you have set up bindings to the web service operation arguments and result object, you must set up an event that invokes/calls the web service operation. You invoke web services with ActionScript code.</P>
<P class=nav-pagination>For this exercise, the ActionScript will execute when a Button fires a click event; web service operations can be fired from any component event. </P>
<DIV class=nav-pagination>
<UL>
<LI>Select the Button in Design or Code view and open the Events tab of the Tag inspector.</LI>
<LI>
<P>Find and select the click event in the list of Button events.</P><IMG class=image-border height=217 alt="When you select Button, the Events panel displays the click event." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig10.gif" width=300> 
<P class=caption><STRONG>Figure 10.</STRONG> When you select Button, the Events panel displays the click event.</P></LI>
<LI>
<P>Click the lightning bolt to display the Select source for <CODE>send()</CODE> dialog box. In the select component to trigger list, select <CODE>service1</CODE>.</P>
<P>When you select <CODE>service1</CODE>, the operations in <CODE>service1</CODE> display in the Select component to trigger list. You want to trigger the <CODE>getList</CODE> operation, so select it. Notice that the Trigger this component/operation field at the bottom of the dialog box displays <CODE>service1.getList</CODE>. </P><IMG class=image-border height=397 alt="The Select source for the <code>send()</code> dialog box with the operation <code>getList()</code> selected." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig11.gif" width=460> 
<P class=caption><STRONG>Figure 11.</STRONG> The Select source for the <CODE>send()</CODE> dialog box with the operation <CODE>getList()</CODE> selected.</P></LI>
<LI>Click OK.</LI>
<LI>
<P>Go to Code view. Notice the added web service operation trigger in the Button’s click event. Save the file. </P>
<DIV class=preWrapper><PRE>&lt;mx:Button label="Get Employee List" click="service1.getList.send()</PRE></DIV></LI></UL></DIV><PRE><!--StartFragment --> <H3>Using the Network Monitor To See SOAP Messages and ActionScript Objects</H3><P>Now that you have set up the bindings and the trigger, it is time to test the application. The Network Monitor helps you debug Flex applications that use data services such as web services, Remote Objects, or HTTP. With the Network Monitor, you can examine the SOAP, AMF, Remote Object, XML, and HTTP traffic as it flows between the Flex application and the data service or services. </P><P>The Network Monitor also supports simple trace statements for basic application debugging. Network Monitor traces, such as the one below, display in the Network Monitor at runtime. </P><DIV class=preWrapper><PRE>NetworkDebugger.NetworkMonitor.trace('trace text goes here');
  </PRE></DIV><OL><LI>Select Window &gt; Network Monitor (Ctrl + Alt + N) to open the Network Monitor.<LI><P>Select the Enable Flex Network Monitor checkbox to activate the Network Monitor.</P><IMG class=image-border height=138 alt="The Network Monitor in the enabled state." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig12.gif" width=460> <P class=caption><STRONG>Figure 12.</STRONG> The Network Monitor in the enabled state.</P><LI>Choose File &gt; Run (F6) or click the Run button on the toolbar to preview the application. As the application loads, see the entries appear in the Network Monitor. <LI><P>In testing this sample application, you aren’t interested in the HTTP traffic, so select the filter icon and deselect HTTP.</P><P class=note><STRONG>Note:</STRONG> You do not lose the data when you deselect a filter, you only hide it. If you need the data, simply select the filter.</P><IMG class=image-border height=146 alt="The Network Monitor after selecting Run, with filtering button selected." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig13.gif" width=460> <P class=caption><STRONG>Figure 13.</STRONG> The Network Monitor after selecting Run, with filtering button selected</P><P>Because you have filtered out the HTTP traffic, you will see the following web service events:</P><UL><LI>First, the initialization of the web service where the web service WSDL is requested <LI>Second, the WSDL that is received in response to the WSDL request</LI></UL><LI><P>Select Product Management in the ComboBox and click the Get Employee List Button to trigger the web service operation. Notice that a few SOAP and web service events appear.</P><P>Selecting the first SOAP event displays the SOAP envelope sent to the web service in the details pane.</P><DIV class=preWrapper><PRE>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
   &lt;SOAP-ENV:Body xmlns:ns1="http://localhost:8400/samples/services/EmployeeWS"&gt;
      &lt;ns1:getList SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
         &lt;deptId xsi:type="xsd:string"&gt;PM&lt;/deptId&gt;
      &lt;/ns1:getList&gt;
   &lt;/SOAP-ENV:Body&gt;
&lt;/SOAP-ENV:Envelope&gt;
</PRE></DIV><P>Selecting the first web service RPC event displays the SOAP envelope as represented in ActionScript.</P><P><STRONG>Method</STRONG>: getList</P><P><STRONG>Parameter 1</STRONG>:<BR><STRONG>name :</STRONG> deptId<BR><STRONG>type :</STRONG> string<BR><STRONG>value :</STRONG> PM</P><P>Selecting the second SOAP event displays the SOAP envelope received from the web service in response to the web service call.</P><DIV class=preWrapper><PRE>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;
   &lt;SOAP-ENV:Body&gt;
      &lt;ns1:getListResponse xmlns:ns1="http://localhost:8300/samples/services/EmployeeWS" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
         &lt;getListResult xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:anyType[2]"&gt;
            &lt;item href="#id0"/&gt;
            &lt;item href="#id1"/&gt;
         &lt;/getListResult&gt;
      &lt;/ns1:getListResponse&gt;
      &lt;multiRef xmlns:ns2="http://www.macromedia.com/samples" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" id="id0" SOAP-ENC:root="0" xsi:type="ns2:Employee"&gt;
         &lt;name xsi:type="xsd:string"&gt;Ronnie Hodgman&lt;/name&gt;
         &lt;phone xsi:type="xsd:string"&gt;555-219-2030&lt;/phone&gt;
         &lt;email xsi:type="xsd:string"&gt;rhodgman@fictitious.com&lt;/email&gt;
         &lt;salary xsi:type="xsd:double"&gt;0.0&lt;/salary&gt;
      &lt;/multiRef&gt;
      &lt;multiRef xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns3="http://www.macromedia.com/samples" id="id1" SOAP-ENC:root="0" xsi:type="ns3:Employee"&gt;
         &lt;name xsi:type="xsd:string"&gt;Joanne Wall&lt;/name&gt;
         &lt;phone xsi:type="xsd:string"&gt;555-219-2012&lt;/phone&gt;
         &lt;email xsi:type="xsd:string"&gt;jwall@fictitious.com&lt;/email&gt;
         &lt;salary xsi:type="xsd:double"&gt;0.0&lt;/salary&gt;
      &lt;/multiRef&gt;
   &lt;/SOAP-ENV:Body&gt;
&lt;/SOAP-ENV:Envelope&gt;
</PRE></DIV><P>Selecting the second web service RPC event displays the response to the web service call as represented in ActionScript.</P><DIV class=preWrapper><PRE>[array] : 
length[Number] : 2
[0][object] : 
SOAP-ENC:root[String] : 0
email[String] : rhodgman@fictitious.com
id[String] : id0
name[String] : Ronnie Hodgman
phone[String] : 555-219-2030
salary[Number] : 0
xmlns:SOAP-ENC[String] : http://schemas.xmlsoap.org/soap/encoding/
xmlns:ns2[String] : http://www.macromedia.com/samples
xsi:type[String] : ns2:Employee

[1][object] : 
SOAP-ENC:root[String] : 0
email[String] : jwall@fictitious.com
id[String] : id1
name[String] : Joanne Wall
phone[String] : 555-219-2012
salary[Number] : 0
xmlns:SOAP-ENC[String] : http://schemas.xmlsoap.org/soap/encoding/
xmlns:ns3[String] : http://www.macromedia.com/samples
xsi:type[String] : ns3:Employee
</PRE></DIV><IMG class=image-border height=137 alt="The Network Monitor after with the web service ActionScript object selected." src="http://www.macromedia.com/devnet/flex/articles/flexbuilder_ws/fig14.gif" width=460> <P class=caption><STRONG>Figure 14.</STRONG> The Network Monitor after with the web service ActionScript object selected.</P></LI></OL><P>Developing and debugging applications can be time consuming. As you have seen, with Flex Builder’s visual data binding you can quickly wire together simple Flex applications. The Network Monitor eases the process of debugging data services in Flex applications, making you more productive.</P><!-- InstanceEndEditable --><DIV class=nav-pagination></DIV></PRE><img src ="http://www.blogjava.net/mstar/aggbug/5990.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mstar/" target="_blank">黑灵</a> 2005-06-12 22:31 <a href="http://www.blogjava.net/mstar/archive/2005/06/12/5990.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>