本人搞C#编程已经有几年了,但Flex一直没有接触过,目前正在自学中,
首先打开VS,本人用的是2005+SP1的版本,创建一个Web应用程序
在默认页Default.aspx中,加入Page_load方法
protected void Page_Load(object sender, EventArgs e) { string str = "HelloWorld"; Response.Write(str); }
好了,Server端建立完毕,简单吧,下面来创建Flex程序客户端
1.打开Flash Builder 4,文件→新建→Flex项目
项目名:自定
应用程序类型:Web(在Adobe Flash Player中运行)
Flex SDK版本:使用默认SDK(当前为“flex4.0”)
应用程序服务器类型:ASP.NET
2.下一步
服务器类型:使用ASP.NET Development Server
3.下一步
4.不做更改,完成
至此一个新的Flex项目建立完毕了
来加个Label吧
5.在<s:Application>内增加<mx:Label id="lblTest"></mx:Label>
6.打开VS,调试运行那个WEB应用程序并 保持运行状态,记录Default.aspx的路径,比如:http://localhost:2717/Default.aspx
7.在Flex项目中的<fx:Declarations>里增加“<mx:HTTPService id="ws" url="http://localhost:2717/Default.aspx" result="ResultHello(event)" ></mx:HTTPService>”,result的意思就是:当httpService访问default.aspx后调用的函数
8.进入设计页面,选中整个Felx工作区,在属性栏的“事件”里有creationComplete方法(相当于aspx里的page_load),单机右侧按钮,增加一个这个事件
protected function application1_creationCompleteHandler(event:FlexEvent):void { ws.send(); }
9.增加ResultHello(event)方法:
public function ResultHello(event:ResultEvent):void { this.txtTest.text = event.result.toString(); }
10.ResultEvent会出编译错误,要增加一个包引用:import mx.rpc.events.ResultEvent;
11.Flex全部代码如下:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)"> <fx:Script> <!--[CDATA[ import mx.events.FlexEvent; import mx.rpc.events.ResultEvent; protected function application1_creationCompleteHandler(event:FlexEvent):void { ws.send(); } public function ResultHello(event:ResultEvent):void{ this.lblTest.text = event.result.toString(); } ]]--> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <mx:HTTPService id="ws" url="http://localhost:2717/Default.aspx" result="ResultHello(event)"> </mx:HTTPService> </fx:Declarations> <mx:Label id="lblTest" > </mx:Label> </s:Application>
12.Ctrl+F11运行吧,出现一个网页,里面是一个flash,上面写着“HelloWorld”