[原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (二)

版权由http://xingfustar.cnblogs.com/所有,转载请注明出处,XingFuStar 2007年12月11日


三、简单数据类型通讯(http://xingfustar.cnblogs.com/)

    学习一门语言,大多以Hello Word开始,我们也以Hello Word为例, 讲解一下Flex 与 Asp.Net 通过 Remoting 方式通讯的方法。

1、.NET服务器端程序(http://xingfustar.cnblogs.com/)
    
    在新建的.NET网站的 App_Code文件夹下,Sample.cs 文件,这是由模板为我们创建的。我们可以防照它,在 App_Code下新建一个 RemotingSample.cs文件,
    
    接下来我们编写一个 HelloWord 函数

public string HelloWord()
{
    
return "Hello Word!";
}

    .NET 端工作暂时告一段落, 接下来我们来试计Flex端

2、Flex客户端程序(http://xingfustar.cnblogs.com/)

    在 Design 模式下,添加一个 Text文本控件,id为txtHelloWord,txt属性为空,添加一个 Button控件,id为btnHelloWord,Label属性为 HelloWord
    在 Source 模式下,加入如下代码

<mx:Script>
        
<![CDATA[
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.controls.Alert;
        
            public function RemoteResult(re:ResultEvent):void
            { 
                var str:String = re.result as String;
                this.txtHelloWord.text = str;
                
            }
        
            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:RemoteObject>
    
    在 mx:Button 标签中添加属性 click="sampleRemoteObject.HelloWord()"

    运行Flex程序,在浏览器中查看效果 

四、带参数的通讯(http://xingfustar.cnblogs.com/)

    有了上面的基础,我们进行下扩展,做一个稍稍复杂些,带参数的方法:

1、.NET服务器端程序(http://xingfustar.cnblogs.com/)

    在.NET服务器端,RemotingSample类中添加一个新的方法:
public string SayHello(string name)
{
    
return "Hello " + name + "!";
}

2、Flex客户端程序(http://xingfustar.cnblogs.com/)

    在 Design 模式下添加,添加一个 Text文本控件,id为txtSayHello,txt属性为空,添加一个 Button控件,id为btnSayHello,Label属性为 SayHello,添加一个Label,text属性为name:,添加一个TextInput, id为txtName
    
    在 Source 模式下, 修改 mx:RemoteObject 标签,添加 
    <mx:method name="SayHello" result="RemoteResult(event)" fault="RemoteFault(event)"/> 
    修改脚本中 RemoteResult 方法,代码如下
            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;
                }
                
            }
    在 mx:Button (SayHello) 标签中添加属性 click="sampleRemoteObject.SayHello(this.txtName.tex)"

    运行Flex程序,在浏览器中查看效果 

附件:完整代码 http://xingfustar.cnblogs.com/)
1、.NET端代码
/*----------------------------------------------------------------
 * 版权:
http://XingFuStar.cnblogs.com
 * 
 * 文件名: RemotingSample
 * 文件功能描述: .NET与Flex通讯DEMO
 * 
 * 作者:XingFuStar
 * 日期:2007年12月11日
 * 
 * 当前版本:V1.0.0
 * 
 * 修改日期:
 * 修改内容:
 *---------------------------------------------------------------
*/

using System;
using com.TheSilentGroup.Fluorine;
using System.Collections.Generic;

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 + "!";
        }
    }
}

2、Flex端MXML代码
<?xml version="1.0" encoding="utf-8"?>
<!--
* 版权:http://XingFuStar.cnblogs.com
*
* 作者:XingFuStar
* 日期:2007年12月11日
-->
<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;
                }
                
            }
        
            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: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:Application>

本节完成!

FAQ:
    一、为什么我在Flex下运行了程序,打开的页面连不上.NET
        查看打开页面的地址,是以“File”开头,还是以“HTTP”开头,想通过Remoting连接,生成的SWF必须运行在服务器模式下,而不是文件模式

版权由http://xingfustar.cnblogs.com/所有,转载请注明出处,XingFuStar 2007年12月11日
posted @ 2007-12-10 17:01  幸福★星  阅读(1631)  评论(1编辑  收藏  举报