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) |
3 |
return "hello , " + p + " , welcome to FluroineFx !" ; |
然后看下在flash中如何调用:
1、既然要先连接到网关,得先有连接对象RemotingConnection.as
03 |
import flash.net.NetConnection; |
04 |
import flash.system.Security; |
07 |
public class RemotingConnection extends NetConnection |
09 |
public function RemotingConnection(gatewayUrl: String ) |
11 |
Security.allowDomain(gatewayUrl); |
12 |
this .objectEncoding = ObjectEncoding.AMF3; |
13 |
this .connect(gatewayUrl); |
允许访问网关url -> 设置编码 -> 打开连接,一气呵成,没啥难理解的
2、as3的调用代码:
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; |
10 |
[SWF(height= 80 , width= 400 )] |
11 |
public class Main extends Sprite |
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; |
21 |
public function Main() |
26 |
private function init(): void |
29 |
lblInput= new TextField(); |
30 |
lblInput.text= "请输入参数:" ; |
31 |
lblInput.selectable= false ; |
32 |
lblInput.autoSize=TextFieldAutoSize.RIGHT; |
39 |
txtInput= new TextField(); |
43 |
txtInput.type=TextFieldType.INPUT; |
44 |
txtInput.text= "菩提树下的杨过" ; |
45 |
txtInput.y=lblInput.y; |
46 |
txtInput.x=lblInput.x + lblInput.width + 5 ; |
49 |
btn= new CustomSimpleButton( 60 , 20 , " 调 用 " ); |
52 |
btn.x=txtInput.x + txtInput.width + 10 ; |
54 |
txtResult= new TextField(); |
56 |
txtResult.x=lblInput.x; |
57 |
txtResult.y=lblInput.y + lblInput.height + 10 ; |
59 |
txtResult.autoSize=TextFieldAutoSize.CENTER; |
60 |
txtResult.textColor= 0xff0000 ; |
61 |
btn.addEventListener(MouseEvent.CLICK, btnClick); |
64 |
private function btnClick(e:MouseEvent): void |
66 |
gateWay.call( "ServiceLib.TestLib.HelloWorld" , new Responder(onResult, onFault), txtInput.text); //调用方法 |
68 |
btn.mouseEnabled= false ; |
72 |
private function onResult(result: String ): void |
74 |
//trace("成功:",result);//成功: hello , 菩提树下的杨过 , welcome to FluroineFx ! |
75 |
txtResult.text=result; |
77 |
btn.mouseEnabled= true ; |
81 |
private function onFault(result: String ): void |
83 |
//trace("失败:",result); |
84 |
txtResult.text=result; |
86 |
btn.mouseEnabled= true ; |
看上去很长,但不用看完,大部分代码是用于创建界面的,关键部分只有注释的几行!
这是运行结果,整个swf不到5k,短小精悍!
再来看下Flex中如何使用:
在flash builder中创建一个flex project,然后把RemotingConnection.as复制到src目录,mxml完整代码如下:
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
12 |
import flash.net.Responder; |
14 |
private var gateWay:RemotingConnection; |
15 |
private var responder:Responder; |
17 |
protected function btnCall_clickHandler(event:MouseEvent):void |
19 |
// TODO Auto-generated method stub |
25 |
gateWay.call("ServiceLib.TestLib.HelloWorld", new Responder(onResult, onFault), txtInput.text); |
26 |
this.btnCall.mouseEnabled = false; |
27 |
this.btnCall.enabled = false; |
30 |
private function onResult(result:String):void |
32 |
txtResult.text=result; |
33 |
this.btnCall.mouseEnabled = true; |
34 |
this.btnCall.enabled = true; |
37 |
private function onFault(result:String):void |
39 |
txtResult.text=result; |
40 |
this.btnCall.mouseEnabled = true; |
41 |
this.btnCall.enabled = true; |
47 |
<!-- Place non-visual elements (e.g., services, value objects) here --> |
61 |
click = "btnCall_clickHandler(event)" /> |
换了个写法而已,逻辑与刚才完全一样!
这是运行界面,看上去好象更专业一点了,但是最终生成的swf 尺寸可就大多了(有所得必有所失)
示例源代码下载: http://cid-2959920b8267aaca.office.live.com/self.aspx/Flash/FluorineFx.rar