[原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (三)
生活随笔
收集整理的這篇文章主要介紹了
[原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (三)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
版權由http://xingfustar.cnblogs.com/所有,轉載請注明出處,XingFuStar 2007年12月14日
五、復雜數據類型的通訊(http://xingfustar.cnblogs.com/)
????
????Remoting支持傳送數組、List、HashTable、Dictionary等多種復雜數據類型,本文以數組,Dictionary,HashTable為例,講解復雜數據類型的通訊。
(一)數組(http://xingfustar.cnblogs.com/)
1、.NET服務器端程序(http://xingfustar.cnblogs.com/)
????假設我們要做段程序,來獲取所有的用戶姓名。修改上節中的代碼,增加一個GetUsers方法。代碼如下:
????????public?string[]?GetArray()
????????{
????????????string[]?array?=?new?string[]{"張三","李四","王五"};
????????????return?array;
????????}
2、Flex客戶端程序(http://xingfustar.cnblogs.com/)
????在 Design 模式下添加,添加一個 Text文本控件,id為txtUsers,txt屬性為空,添加一個 Button控件,id為btnGetArray,Label屬性為?GetArray
????
????在 Source 模式下, 修改 mx:RemoteObject 標簽,添加
????<mx:method name="GetArray" result="RemoteResult(event)" fault="RemoteFault(event)"/>?
????修改腳本中 RemoteResult 方法,增加一個Case條件,代碼如下
????????????public?function?RemoteResult(re:ResultEvent):void
????????????{?
????????????????switch(re.currentTarget.name)
????????????????{
????????????????????case?"HelloWord":
????????????????????????var?str:String?=?re.result?as?String;
????????????????????????this.txtHelloWord.text?=?str;
????????????????????????break;
????????????????????case?"SayHello":
????????????????????????str?=?re.result?as?String;
????????????????????????this.txtSayHello.text?=?str;
????????????????????????break;
????????????????????case?"GetArray":
????????????????????????for(var?i:int=0;?i<re.result.length;?i++)
????????????????????????{
????????????????????????????this.txtUsers.text?+=?re.result[i].toString()?+?",?";
????????????????????????}
????????????????????????break;
????????????????}
????????????} ????在 mx:Button (GetArray)?標簽中添加屬性 click="sampleRemoteObject.GetArray()"
????運行Flex程序,在瀏覽器中查看效果
(二)Dictionary(http://xingfustar.cnblogs.com/)
1、.NET服務器端程序(http://xingfustar.cnblogs.com/)
????接下來我們做個Dictionary的例子,假設上例中,我們不光要獲取用戶姓名,而且我們還要得到他對應的年齡,我們使用Dictionary。修改代碼,添加如下方法:
????????public?Dictionary<String,?Int32>?GetDictionary()
????????{
????????????Dictionary<String,?Int32>?age?=?new?Dictionary<string,?int>();
????????????age.Add("張三",?23);
????????????age.Add("李四",?24);
????????????age.Add("王五",?22);
????????????return?age;
????????}
2、Flex客戶端程序(http://xingfustar.cnblogs.com/)
????在 Design 模式下添加,添加六個 Text文本控件,屬性分別為 id = txtZhangSan,txt = ""; id = txtLiSi,txt = ""; id = txtWangWu,txt = ""; txt = "張三:"; txt = "李四:"; txt = "王五:"
????添加一個 Button控件,id為btnGetDictionary,Label屬性為?GetDictionary
????
????在 Source 模式下, 修改 mx:RemoteObject 標簽,添加
????<mx:method name="GetDictionary" result="RemoteResult(event)" fault="RemoteFault(event)"/>?
????修改腳本中 RemoteResult 方法,增加一個Case條件,代碼如下
????????????public?function?RemoteResult(re:ResultEvent):void
????????????{?
????????????????switch(re.currentTarget.name)
????????????????{
????????????????????case?"HelloWord":
????????????????????????var?str:String?=?re.result?as?String;
????????????????????????this.txtHelloWord.text?=?str;
????????????????????????break;
????????????????????case?"SayHello":
????????????????????????str?=?re.result?as?String;
????????????????????????this.txtSayHello.text?=?str;
????????????????????????break;
????????????????????case?"GetUsers":
????????????????????????for(var?i:int=0;?i<re.result.length;?i++)
????????????????????????{
????????????????????????????this.txtUsers.text?+=?re.result[i].toString()?+?",?";
????????????????????????}
????????????????????????break;
????????????????????case?"GetDictionary":
????????????????????????this.txtZhangSan.text?=?re.result["張三"];
????????????????????????this.txtLiSi.text?=?re.result["李四"];
????????????????????????this.txtWangWu.text?=?re.result["王五"];
????????????????????????break;
????????????????}
????????????} ????在 mx:Button (GetDictionary)?標簽中添加屬性 click="sampleRemoteObject.GetDictionary()"
????運行Flex程序,在瀏覽器中查看效果
(三)HashTable(http://xingfustar.cnblogs.com/)
1、.NET服務器端程序(http://xingfustar.cnblogs.com/)
????我們將上例中的Dictionary類型改成HashTable類型,代碼如下:
????????public?Hashtable?GetHashTable()
????????{
????????????Hashtable?hash?=?new?Hashtable();
????????????hash.Add("張三",?23);
????????????hash.Add("李四",?24);
????????????hash.Add("王五",?22);
????????????return?hash;
????????}
2、Flex客戶端程序(http://xingfustar.cnblogs.com/)
????借用下上例內容,在 Design 模式下添加一個 Button控件,id為btnGetHashTable,Label屬性為?GetHashTable
????
????在 Source 模式下, 修改 mx:RemoteObject 標簽,添加
????<mx:method name="GetHashTable" result="RemoteResult(event)" fault="RemoteFault(event)"/>?
????修改腳本中 RemoteResult 方法,只需在上例? case "GetDictionary": 下一行加上 case "GetHashTable": 即可。
????????????????????case?"GetDictionary":
????????????????????case?"GetHashTable":
????????????????????????this.txtZhangSan.text?=?re.result["張三"];
????????????????????????this.txtLiSi.text?=?re.result["李四"];
????????????????????????this.txtWangWu.text?=?re.result["王五"];
????????????????????????break;
????在 mx:Button (GetHashTable)?標簽中添加屬性 click="sampleRemoteObject.GetHashTable()"
????運行Flex程序,在瀏覽器中查看效果
附件:完整代碼(http://xingfustar.cnblogs.com/)
1、.NET端
?*?版權:http://XingFuStar.cnblogs.com
?*?
?*?文件名:?RemotingSample
?*?文件功能描述:?.NET與Flex通訊DEMO
?*?
?*?作者:XingFuStar
?*?日期:2007年12月11日
?*?
?*?當前版本:V1.0.0
?*?
?*?修改日期:2007年12月14日
?*?修改內容:增加獲取數組,Dictionary,HashTable等方法例程
?*---------------------------------------------------------------*/
using?System;
using?com.TheSilentGroup.Fluorine;
using?System.Collections.Generic;
using?System.Collections;
namespace?RemotingSample
{
????[RemotingService("Fluorine?sample?service")]
????public?class?RemotingSample
????{
????????public?RemotingSample()
????????{
????????????//請不要刪除以下信息
????????????//版權:http://XingFuStar.cnblogs.com
????????}
????????public?string?HelloWord()
????????{
????????????return?"Hello?Word!";
????????}
????????public?string?SayHello(string?name)
????????{
????????????return?"Hello?"?+?name?+?"!";
????????}
????????public?string[]?GetArray()
????????{
????????????string[]?array?=?new?string[]{"張三","李四","王五"};
????????????return?array;
????????}
????????public?Dictionary<String,?Int32>?GetDictionary()
????????{
????????????Dictionary<String,?Int32>?dictionary?=?new?Dictionary<string,?int>();
????????????dictionary.Add("張三",?23);
????????????dictionary.Add("李四",?24);
????????????dictionary.Add("王五",?22);
????????????return?dictionary;
????????}
????????public?Hashtable?GetHashTable()
????????{
????????????Hashtable?hash?=?new?Hashtable();
????????????hash.Add("張三",?23);
????????????hash.Add("李四",?24);
????????????hash.Add("王五",?22);
????????????return?hash;
????????}
????}
}
2、Flex端
<?xml?version="1.0"?encoding="utf-8"?><!--
*?版權:http://XingFuStar.cnblogs.com
*
*?作者:XingFuStar
*?日期:2007年12月11日
*
*?修改日期:2007年12月14日
*?修改內容:增加獲取數組,Dictionary,HashTable等方法例程。
-->
<mx:Application?xmlns:mx="http://www.adobe.com/2006/mxml"?layout="absolute">
????<mx:Script>
????????<![CDATA[
????????????import?mx.rpc.events.FaultEvent;
????????????import?mx.rpc.events.ResultEvent;
????????????import?mx.controls.Alert;
????????
????????????public?function?RemoteResult(re:ResultEvent):void
????????????{?
????????????????switch(re.currentTarget.name)
????????????????{
????????????????????case?"HelloWord":
????????????????????????var?str:String?=?re.result?as?String;
????????????????????????this.txtHelloWord.text?=?str;
????????????????????????break;
????????????????????case?"SayHello":
????????????????????????str?=?re.result?as?String;
????????????????????????this.txtSayHello.text?=?str;
????????????????????????break;
????????????????????case?"GetArray":
????????????????????????for(var?i:int=0;?i<re.result.length;?i++)
????????????????????????{
????????????????????????????this.txtUsers.text?+=?re.result[i].toString()?+?",?";
????????????????????????}
????????????????????????break;
????????????????????case?"GetDictionary":
????????????????????case?"GetHashTable":
????????????????????????this.txtZhangSan.text?=?re.result["張三"];
????????????????????????this.txtLiSi.text?=?re.result["李四"];
????????????????????????this.txtWangWu.text?=?re.result["王五"];
????????????????????????break;
????????????????}
????????????}
????????
????????????public?function?RemoteFault(re:FaultEvent):void
????????????{
????????????????Alert.show("Message:"?+?re.fault.faultString,"出錯");
????????????}???????????
????????]]>
????</mx:Script>
????<!--這里Source?對應.NET類,前面是命名空間,后面是類名?source?=?命名空間.類名-->
????<mx:RemoteObject?
????????id="sampleRemoteObject"
????????destination="fluorine"
????????source="RemotingSample.RemotingSample"
????????showBusyCursor="true">
????????<!--這里是.NET中的方法,name?=?方法名?-->
????????<mx:method?name="HelloWord"?result="RemoteResult(event)"?fault="RemoteFault(event)"/>????
????????<mx:method?name="SayHello"?result="RemoteResult(event)"?fault="RemoteFault(event)"/>????????????
????????<mx:method?name="GetArray"?result="RemoteResult(event)"?fault="RemoteFault(event)"/>
????????<mx:method?name="GetDictionary"?result="RemoteResult(event)"?fault="RemoteFault(event)"/>????
????????<mx:method?name="GetHashTable"?result="RemoteResult(event)"?fault="RemoteFault(event)"/>????????
????</mx:RemoteObject>
????<mx:Text?x="38"?y="25"?id="txtHelloWord"/>
????<mx:Button?x="38"?y="51"?label="HelloWord"?id="btnHelloWord0"?click="sampleRemoteObject.HelloWord()"/>
????
????<mx:Text?x="38"?y="105"?id="txtSayHello"/>
????<mx:Label?x="38"?y="131"?text="name:"/>
????<mx:TextInput?x="88"?y="129"?id="txtName"/>
????<mx:Button?x="256"?y="129"?label="SayHello"?id="btnSayHello"?click="sampleRemoteObject.SayHello(this.txtName.text)"/>
????
????<mx:Text?x="38"?y="181"?id="txtUsers"/>
????<mx:Button?x="38"?y="207"?label="GetArray"?id="btnGetArray"?click="sampleRemoteObject.GetArray()"/>
????
????<mx:Label?x="38"?y="262"?text="張三:"/>
????<mx:Text?x="76"?y="262"?id="txtZhangSan"/>
????<mx:Label?x="129"?y="262"?text="李四:"/>
????<mx:Text?x="167"?y="262"?id="txtLiSi"/>
????<mx:Label?x="218"?y="262"?text="王五:"/>
????<mx:Text?x="256"?y="262"?id="txtWangWu"/>
????<mx:Button?x="38"?y="288"?label="GetDictionary"?id="btnGetDictionary"?click="sampleRemoteObject.GetDictionary()"/>
????<mx:Button?x="157"?y="288"?label="GetHashTable"?id="btnGetHashTable"?click="sampleRemoteObject.GetHashTable()"/>
</mx:Application>
本節內容到此結束,其實本節的例子都差不多,相信講到這里,看過文章的人對Remoting通訊有了大概的了解,只要熟悉AS3語言,以上這些都不困難,關于這個通訊,我計劃還有一節,主要講解自定義實體對象的傳送。
如可我的文章對您有幫助,希望在轉載時保留版權及出處,謝謝!網友的支持是我最大的動力!
版權由http://xingfustar.cnblogs.com/所有,轉載請注明出處,XingFuStar 2007年12月14日
轉載于:https://www.cnblogs.com/XingfuStar/archive/2007/12/14/994875.html
總結
以上是生活随笔為你收集整理的[原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《大话设计模式》第29章-OOTV杯超级
- 下一篇: 第七章 类图