flex 與asp.net 配合之道

來源:互聯網
上載者:User
1.將flex編譯後的程式插入到asp.net頁面

flex的最終輸出就是一張網頁+一個flash(.swf檔案)
就是用他產生的網頁的方式把那個.swf檔案插入asp.net頁面就可以了。

flex3項目名字叫TestApp,最簡單直接的辦法就是,
把"bin-debug"目錄下的:
TestApp.html
TestApp.swf
AC_OETags.js
playerProductInstall.swf
這4個檔案複製到asp.net網站下面,開啟TestApp.html,把內容複寫到asp.net程式頁面(.aspx檔案)中。
比如Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
//把TestApp.html的內容全部複製到這裡
//....
//...

總而言之FLEX3最後編譯成了一個.swf檔案而已,這個檔案在網站裡面插入的方法和普通的flash動畫的那種.swf檔案的使用方法是一樣的。

還有其他的要求:flex3程式和網頁還有互動,請用"flex externalinterface"搜尋

2.flex程式與asp.net程式互動

可以使用flex的Loader往asp.net發送請求,擷取xml。
也可以使用ExternalInterface和網頁中的js互動,讓js發送ajax請求到asp.net。

下面有一執行個體,目標是:在flex端將資料Post到asp.net頁面中,並將返回的xml資料顯示出來

//Asp.net端代碼
//getxml.aspx代碼,保留一行即可,刪除其他的html代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="getxml.aspx.cs" Inherits="getxml" %>

//getxml.aspx.cs
//using System...
using System.Xml;
public partial class getxml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string user_pkid = System.Web.HttpContext.Current.Request.Form["user_pkid"];
        if user_pkid != null)
        {
             CreateXml();//建立Xml的方法,可使用XmlTextWriter、XmlDocument ,或者直接讀取Xml檔案等待
        }
    }

    private void CreateXml()
    {
        XmlDocument doc = new XmlDocument();
        XmlNode root = doc.CreateElement("channel");

        XmlElement titleElm = doc.CreateElement("title");
        titleElm.InnerText = "blogweather";

        //...
   
        root.AppendChild(titleElm);
        doc.AppendChild(root);

        XmlTextWriter xw = new XmlTextWriter(Response.OutputStream,System.Text.Encoding.UTF8);//寫到頁面傳回值中
        xw.Formatting = Formatting.Indented;//將Xml格式化
        doc.Save(xw);
        xw.Flush();
        xw.Close();
    }
}

Xml資料如下:
<?xml version="1.0" encoding="UTF-8" ?>
<channel>
    <title>blogweather</title>
    <link>http://www.blogweather.net</link>
    <description>部落格天氣預報</description>
</channel>

方法一:
如果所有值均在xml資料中,而且不需要拿這些資料做二次分析,則推薦使用 HTTPService控制項

Flex 端代碼:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()" >
 <mx:Script>
  <![CDATA[
   import mx.messaging.AbstractConsumer;
   import flash.events.MouseEvent;
   import mx.controls.Alert;
      
   private function init():void
   {
    getxml.url = "http://www.blogweather.net/getxml.aspx"; //接收Post方法的頁面
    var data:Object = new Object();
    data["user_pkid"] = this.parameters.user_pkid;
    getxml.send(data);
   }
            ]]>
 </mx:Script>
 <mx:HTTPService id="getxml" showBusyCursor="true" useProxy="false" method="POST">
 </mx:HTTPService>
 <mx:TextArea  wordWrap="true" editable="false" enabled="true" id="lb_title">
  <mx:text>{getxml.lastResult.channel.title}</mx:text>
 </mx:TextArea>
</mx:Application>

方法二:
如果要將資料進行分析,則要使用URLLoader和URLRequest
Flex 端代碼:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init();">
 <mx:Script>
  <![CDATA[
   import mx.messaging.AbstractConsumer;
   import mx.messaging.channels.StreamingAMFChannel;
   import flash.events.MouseEvent;
   import mx.controls.Alert;
      
             public var myLoader:URLLoader = new URLLoader();
             public var myRequest:URLRequest;
             public var user_pkid:String;

   private function init():void
   {
    var http://www.cnblogs.com/glaivelee/admin/String = "http://www.blogweather.net/getxml.aspx";
    myRequest = new URLRequest(url);
    myRequest.method = URLRequestMethod.POST;
    var data:URLVariables = new URLVariables();
    //接收來自flash的參數調用,比如flash檔案為 loadxml.swf,帶參數 loadxml.swf?user_pkid=10001
    data.user_pkid = this.parameters.user_pkid; // 擷取10001
    myRequest.data = data;
    myLoader.load(myRequest);
    myLoader.addEventListener(Event.COMPLETE,onLoadComplete);
   }
   
   private function onLoadComplete(event:Event):void
   {
    var myxml:XML;
    var loader:URLLoader = URLLoader(event.target);
    myxml = new XML(loader.data);
    
    lb_title.text =myxml.child("channel")[0].child("title");
    if( lb_title.text == "blogweather")
    {
     Alert("頁面名稱為:部落格天氣預報");
    }
   }
   
  ]]>
 </mx:Script>
 <mx:TextArea  wordWrap="true" editable="false" enabled="true" id="lb_title">
  <mx:text>lb_title</mx:text>
 </mx:TextArea>
</mx:Application>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.