本人搞C#編程已經有幾年了,但Flex一直沒有接觸過,目前正在自學中,
首先開啟VS,本人用的是2005+SP1的版本,建立一個Web應用程式
在預設頁Default.aspx中,加入Page_load方法
protected void Page_Load(object sender, EventArgs e)<br />{<br /> string str = "HelloWorld";<br /> Response.Write(str);<br />}
好了,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<br />{<br />ws.send();<br />}
9.增加ResultHello(event)方法:
public function ResultHello(event:ResultEvent):void<br />{<br />this.txtTest.text = event.result.toString();<br />}
10.ResultEvent會出編譯錯誤,要增加一個包引用:import mx.rpc.events.ResultEvent;
11.Flex全部代碼如下:
<?xml version="1.0" encoding="utf-8"?><br /><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"<br /> xmlns:s="library://ns.adobe.com/flex/spark"<br /> xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)"><br /><fx:Script><br /><!--[CDATA[<br />import mx.events.FlexEvent;<br />import mx.rpc.events.ResultEvent;</p><p> protected function application1_creationCompleteHandler(event:FlexEvent):void<br />{<br />ws.send();<br />}<br />public function ResultHello(event:ResultEvent):void{<br />this.lblTest.text = event.result.toString();<br />}</p><p>]]--><br /></fx:Script><br /><fx:Declarations><br /><!-- 將非可視元素(例如服務、值對象)放在此處 --><br /><mx:HTTPService id="ws" url="http://localhost:2717/Default.aspx" result="ResultHello(event)"></p><p></mx:HTTPService><br /></fx:Declarations></p><p><mx:Label id="lblTest" ></p><p></mx:Label><br /></s:Application>
12.Ctrl+F11運行吧,出現一個網頁,裡面是一個flash,上面寫著“HelloWorld”