shancheng44 发表于 2013-2-1 12:08:10

SmarInvoke——实现C#与Flex方便的互调(sliverlight以外的另一选择)

一C#调用flex1:创建C#需要调用的swf 1.1:打开flexBuilder创建名称为DemoCs1的web application并导入SmartInvoke2009-12-2.0.swc库 如下图:http://img.pusuo.net/2010-01-14/111072264.jpg
.2:在DemoCs1项目中创建test.Hello类,以便C#的调用如下图 :
http://img.pusuo.net/2010-01-14/111072265.jpg
1.3:在DemoCs1.mxml初始化smartinvoke并引用test.Hello类,如下图:
http://img.pusuo.net/2010-01-14/111072266.jpg
2:用C#编写winform程序调用前面生成的swf
 2.1:打开vs创建名为Demo1的winform程序,然后引用进cn.smartinvoke2009-12-1.0.dll库,如下图:
2.2:从工具箱中将flash active拖到form1中,如下图: 
http://img.pusuo.net/2010-01-14/111072267.jpg
2.3:在Form的load事件中创建flash active接口的包装类FlashContainer的对象如下图:
http://img.pusuo.net/2010-01-14/111072269.jpg
2.4:获得刚才生成的swf的绝对路径,在C#加载此swf到axShockwaveFlash,如下图
 
 
2.5:创建与flex中test.Hello服务类对应的代理类test.Hello
http://img.pusuo.net/2010-01-14/111072268.jpg
 2.6:调用FlashContainer LoadComplete代理,当flash加载完毕后调用loadComplete方法,实现对flex的 test.Hello类的调用
http://img.pusuo.net/2010-01-14/111072270.jpg
二:flex调用C#
1:创建C#的test.CsServer服务类,提供hello服务方法,代码如下:
 
using System;using System.Collections.Generic;using System.Text;using cn.smartinvoke.csflex;using System.Windows.Forms;         namespace test{      /// <summary>      /// 实现IServerObject接口的C#的服务类,专门接受flex的调用      /// </summary>      public class CsServer : IServerObject      {          public String hello(string str)          {            MessageBox.Show(str);            return "你好flex,我是C#服务";          }      }}      using System;   using System.Collections.Generic;   using System.Text;   using cn.smartinvoke.csflex;   using System.Windows.Forms;            namespace test   {       /// <summary>       /// 实现IServerObject接口的C#的服务类,专门接受flex的调用       /// </summary>       public class CsServer : IServerObject       {         public String hello(string str)         {               MessageBox.Show(str);               return "你好flex,我是C#服务";         }       }   }  
using System; using System.Collections.Generic;using System.Text;using cn.smartinvoke.csflex; using System.Windows.Forms;namespace test { /// <summary> /// 实现IServerObject接口的C#的服务类,专门接受flex的调用 /// </summary> public class CsServer : IServerObject { public String hello(string str) { MessageBox.Show(str); return "你好flex,我是C#服务"; } }}  
2:在flex中创建C#中test.CsServer服务类的代理类:test.CsServer,具体代码如下:
 
package test{      import cn.smartinvoke.RemoteObject;      /**   *C# test.CsServer对应的服务类,负责对该服务类调用的封装。   */      public class CsServer extends RemoteObject      {          public function CsServer()          {            this.createRemoteObject();          }          /**          *hello方法的代理方法          */          public function hello(str:String):String{            var retObj:Object=this.call("hello",arguments);            return retObj as String;          }      }} 
package test   {       import cn.smartinvoke.RemoteObject;       /**      *C# test.CsServer对应的服务类,负责对该服务类调用的封装。      */      public class CsServer extends RemoteObject       {         public function CsServer()         {               this.createRemoteObject();         }         /**         *hello方法的代理方法         */          public function hello(str:String):String{               var retObj:Object=this.call("hello",arguments);               return retObj as String;         }       }   } 
package test { import cn.smartinvoke.RemoteObject; /** *C# test.CsServer对应的服务类,负责对该服务类调用的封装。 */ public class CsServer extends RemoteObject { public function CsServer() { this.createRemoteObject(); } /** *hello方法的代理方法 */ public function hello(str:String):String{ var retObj:Object=this.call("hello",arguments); return retObj as String; } } } 3:在DemoCs1.mxml中调用test.CsServer代理类,以实现对C#服务类的调用,具体代码如下:
 
<?xml version="1.0" encoding="utf-8"?>lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"   creationComplete="init()"   layout="vertical"><mx:Script>      <!]></mx:Script>lt;/mx:Application> 
 
<?xml version="1.0" encoding="utf-8"?>lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"   creationComplete="init()"   layout="vertical"><mx:Script>      <!]></mx:Script>lt;/mx:Application>  
 
http://img.pusuo.net/2010-01-14/111072271.jpg
 
页: [1]
查看完整版本: SmarInvoke——实现C#与Flex方便的互调(sliverlight以外的另一选择)