c# winform as3相互呼叫

☆綠茶☆發表於2014-10-27

C#主要程式碼:

首先要新增COM元件-Shockwave Flash Object

        //接收flash傳送過來的資訊
        private void axShockwaveFlash1_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(e.request);

            XmlNodeList list = document.GetElementsByTagName("arguments");
            LoadTitleAndAuthor(Convert.ToString(list[0].FirstChild.InnerText), Convert.ToString(list[0].ChildNodes[1].InnerText));
        }
        public void LoadTitleAndAuthor(string title, string author)
        {
           textBox2.Text=title + "," + author;
        }
        //向flash傳送資訊
        private void sendToFlash_Click(object sender, EventArgs e)
        {
            this.axShockwaveFlash1.CallFunction("<invoke name=\"loadMsg\" returntype=\"xml\"><arguments><string>" +
                this.textBox1.Text + "</string><string>男<string></arguments></invoke>");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.axShockwaveFlash1.Movie = Application.StartupPath + "/未命名-4.swf";
        }

flash檔案主要程式碼as2.0:

接收有C#傳送過來的訊息

import flash.external.ExternalInterface;

ExternalInterface.addCallback("loadMsg",null,loadMsg);

function loadMsg(t1:String,t2:String):Void{
        txtName.text=t1;
  txtAge.text=t2;
 }

向C#窗體傳送訊息:

on(release)
{
 import flash.external.ExternalInterface;
 //trace("metadata received");
 ExternalInterface.call("loadMsg", txtName.text,txtAge.text);
}

C#與flash as3.0互動

as3.0 類檔案編寫 

package{
    import flash.display.*;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.getDefinitionByName;
    import flash.external.ExternalInterface;
    import flash.text.*;

    public class Clas extends MovieClip
    {
        public function Clas()
        {
            btnsend.addEventListener(MouseEvent.CLICK,onclick);
            ExternalInterface.addCallback("setTxt",setTxt);
        }
        
        public function onclick(evt:MouseEvent){
            ExternalInterface.call("toTxt",this.txtsend.text);
        }
        
        public function setTxt(s:String,s2:String)
        {
            this.txtmsg.text=s+"-"+s2;
        }
    }
    
    
    } 

 C#程式碼的編寫與as2.0通訊的一樣。

 

http://www.cnblogs.com/Xingsoft-555/archive/2010/05/09/1731221.html

相關文章