flex 与 FluorineFx通讯之Hello World!

Bēniaǒk兄弟的Flex与.NET互操作(六):Flex和.NET协同开发利器FluorineFx 是基于vs2008 + flex builder3的,不知道什么原因,我在vs2010 + flash builder4 上试了几次,总是不成功(也许晚上应该自我检讨下人品鸟),于是有了这一篇东东,算是对 vs2010/flash builder4环境下的一个补充吧

.net的服务端依照参照silverlight获取外部数据的另一种选择:FluorineFx 里的做法,在TestLib.cs里定义一个方法:

1 public string HelloWorld(string p)
2 {
3             return "hello , " + p + " , welcome to FluroineFx !";
4         }

然后看下在flash中如何调用:

1、既然要先连接到网关,得先有连接对象RemotingConnection.as

01 package
02 {
03     import flash.net.NetConnection;
04     import flash.system.Security;
05     import flash.net.*;
06  
07     public class RemotingConnection extends NetConnection
08     {
09         public function RemotingConnection(gatewayUrl:String)
10         {
11             Security.allowDomain(gatewayUrl);
12             this.objectEncoding = ObjectEncoding.AMF3;
13             this.connect(gatewayUrl);          
14         }
15     }
16 }

允许访问网关url -> 设置编码 -> 打开连接,一气呵成,没啥难理解的

2、as3的调用代码:

01 package
02 {
03     import flash.display.Sprite;
04     import flash.events.MouseEvent;
05     import flash.net.Responder;
06     import flash.text.TextField;
07     import flash.text.TextFieldAutoSize;
08     import flash.text.TextFieldType;
09  
10     [SWF(height=80, width=400)]
11     public class Main extends Sprite
12     {
13         private var gateWay:RemotingConnection;
14         private var responder:Responder;
15         private var txtInput:TextField;
16         private var txtResult:TextField;
17         private var lblInput:TextField;
18         private var btn:CustomSimpleButton;
19         private var btnLabel:TextField;
20  
21         public function Main()
22         {
23             init();
24         }
25  
26         private function init():void
27         {
28             gateWay=new RemotingConnection(<a href="http://localhost:1718/Gateway.aspx">http://localhost:1718/Gateway.aspx</a>); //创建连接对象
29             lblInput=new TextField();
30             lblInput.text="请输入参数:";
31             lblInput.selectable=false;
32             lblInput.autoSize=TextFieldAutoSize.RIGHT;
33             addChild(lblInput);
34             lblInput.height=20;
35             lblInput.x=10;
36             lblInput.y=10;
37             addChild(lblInput);
38  
39             txtInput=new TextField();
40             txtInput.border=true;
41             txtInput.width=200;
42             txtInput.height=20;
43             txtInput.type=TextFieldType.INPUT;
44             txtInput.text="菩提树下的杨过";
45             txtInput.y=lblInput.y;
46             txtInput.x=lblInput.x + lblInput.width + 5;
47             addChild(txtInput);
48  
49             btn=new CustomSimpleButton(6020" 调 用 ");
50             addChild(btn);
51             btn.y=txtInput.y;
52             btn.x=txtInput.x + txtInput.width + 10;
53  
54             txtResult=new TextField();
55             addChild(txtResult);
56             txtResult.x=lblInput.x;
57             txtResult.y=lblInput.y + lblInput.height + 10;
58             txtResult.width=380;
59             txtResult.autoSize=TextFieldAutoSize.CENTER;
60             txtResult.textColor=0xff0000;
61             btn.addEventListener(MouseEvent.CLICK, btnClick);
62         }
63  
64         private function btnClick(e:MouseEvent):void
65         {
66             gateWay.call("ServiceLib.TestLib.HelloWorld"newResponder(onResult, onFault), txtInput.text);//调用方法
67             btn.enabled=false;
68             btn.mouseEnabled=false;
69         }
70  
71         //成功的回调函数
72         private function onResult(result:String):void
73         {
74             //trace("成功:",result);//成功: hello , 菩提树下的杨过 , welcome to FluroineFx !
75             txtResult.text=result;
76             btn.enabled=true;
77             btn.mouseEnabled=true;
78         }
79  
80         //失败的回调函数
81         private function onFault(result:String):void
82         {
83             //trace("失败:",result);
84             txtResult.text=result;
85             btn.enabled=true;
86             btn.mouseEnabled=true;
87         }
88     }
89 }

看上去很长,但不用看完,大部分代码是用于创建界面的,关键部分只有注释的几行!

这是运行结果,整个swf不到5k,短小精悍!

再来看下Flex中如何使用:

在flash builder中创建一个flex project,然后把RemotingConnection.as复制到src目录,mxml完整代码如下:

01 <?xml version="1.0" encoding="utf-8"?>
02 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
03                xmlns:s="library://ns.adobe.com/flex/spark"
04                xmlns:mx="library://ns.adobe.com/flex/mx"
05                minWidth="955"
06                minHeight="600"
07                width="299"
08                height="188">
09  
10     <fx:Script>
11         <![CDATA[
12             import flash.net.Responder;
13  
14             private var gateWay:RemotingConnection;
15             private var responder:Responder;
16  
17             protected function btnCall_clickHandler(event:MouseEvent):void
18             {
19                 // TODO Auto-generated method stub
20  
21                 if (gateWay == null)
22                 {
23                     gateWay=new RemotingConnection("http://localhost:1718/Gateway.aspx");
24                 }
25                 gateWay.call("ServiceLib.TestLib.HelloWorld", new Responder(onResult, onFault), txtInput.text);
26                 this.btnCall.mouseEnabled = false;
27                 this.btnCall.enabled = false;
28             }
29  
30             private function onResult(result:String):void
31             {              
32                 txtResult.text=result;
33                 this.btnCall.mouseEnabled = true;
34                 this.btnCall.enabled = true;
35             }
36  
37             private function onFault(result:String):void
38             {              
39                 txtResult.text=result;
40                 this.btnCall.mouseEnabled = true;
41                 this.btnCall.enabled = true;
42             }
43         ]]>
44     </fx:Script>
45  
46     <fx:Declarations>
47         <!-- Place non-visual elements (e.g., services, value objects) here -->
48     </fx:Declarations>
49     <s:Label x="10"
50              y="10"
51              text="请输入参数:"
52              id="lblInput"/>
53     <s:TextInput x="88"
54                  y="6"
55                  id="txtInput"
56                  text="菩提树下的杨过"/>
57     <s:Button x="224"
58               y="7"
59               label="提 交"
60               id="btnCall"
61               click="btnCall_clickHandler(event)"/>
62     <s:TextArea x="6"
63                 y="36"
64                 width="287"
65                 height="145"
66                 id="txtResult"/>
67 </s:Application>

换了个写法而已,逻辑与刚才完全一样!

这是运行界面,看上去好象更专业一点了,但是最终生成的swf 尺寸可就大多了(有所得必有所失)

示例源代码下载: http://cid-2959920b8267aaca.office.live.com/self.aspx/Flash/FluorineFx.rar

作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
Tag标签: flash,flex,fluorineFx

posted on 2010-11-17 15:09 aiaiwoo 阅读(265) 评论(0)  编辑  收藏 所属分类: AC3/FLEX


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿

随笔分类

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜