终于试出flex用WebService的方式取得组件的数据系结。
后端用Turbo C#开新WebService专案,贴上下面二个简单的Method
[WebMethod]
public string [] HelloWorld()
{
string [] arr=new string[3];
arr[0]="字符串A";
arr[1]="字符串二";
arr[2]="字符串三";
return arr;
}
[WebMethod]
public double Calc(double r)
{
return 2 * r * Math.PI;
}
然后用 Cassini web server方式执行。前端用flex MXML Application贴上下面程序代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var myData:ArrayCollection;
private function resultHandler(event:ResultEvent):void {
myData = event.result as ArrayCollection;
}
]]>
</mx:Script>
<mx:WebService id="myService"
wsdl="http://127.0.0.1:8080/WebServiceApplication3/WebService1.asmx?wsdl">
<mx:operation name="HelloWorld" result="resultHandler(event)"/>
<mx:operation name="Calc">
<mx:request>
<r>{radius.text}</r>
</mx:request>
</mx:operation>
</mx:WebService>
<mx:List id="myList" dataProvider="{myData}" width="206"/>
<mx:Label text="{myList.selectedItem}"/>
<mx:TextInput id="radius"/>
<mx:Button label="取得数组" click="myService.HelloWorld.send()"/>
<mx:Text text="{myService.Calc.lastResult}" />
<mx:Button label="计算" click="myService.Calc.send();"/>
</mx:Application>
执行之后,便用WebService方式向后端取得HelloWorld及Calc二个method的回传值。关键点在lastResult若是数组需以as ArrayCollection转型成ArrayCollection来承接。
本文介绍了一种使用Flex前端与后端TurboC# WebService进行交互的方法。通过具体示例展示了如何从前端调用后端的HelloWorld和Calc两个方法,并获取返回值。
1776

被折叠的 条评论
为什么被折叠?



