Flex2.0和C#间的通信[转]
http://blog.ezse.com/trackback.asp?tbID=299
C#的代码:
![程序代码](http://blog.ezse.com/images/code.gif)
private void button1_Click(object sender, EventArgs e)
{
String request = "<invoke name=\"testExternalAPI\" returntype=\"xml\"><arguments><string>Sent by C#</string></arguments></invoke>";
try
{
this.flashResponse_txt.Text = this.flashControl.CallFunction(request);
}
catch
{
this.flashResponse_txt.Text = e.ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
String swfPath = @"M:\ECX\FLASH\bin\Desktop.swf";
this.flashControl.LoadMovie(0, swfPath);
}
private void flashControl_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
{
this.flashRequest_txt.Text = e.request;
MessageBox.Show(e.request);
this.flashControl.SetReturnValue("<string>C# Return 的内容</string>");
}
{
String request = "<invoke name=\"testExternalAPI\" returntype=\"xml\"><arguments><string>Sent by C#</string></arguments></invoke>";
try
{
this.flashResponse_txt.Text = this.flashControl.CallFunction(request);
}
catch
{
this.flashResponse_txt.Text = e.ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
String swfPath = @"M:\ECX\FLASH\bin\Desktop.swf";
this.flashControl.LoadMovie(0, swfPath);
}
private void flashControl_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
{
this.flashRequest_txt.Text = e.request;
MessageBox.Show(e.request);
this.flashControl.SetReturnValue("<string>C# Return 的内容</string>");
}
Flex 的代码:
![程序代码](http://blog.ezse.com/images/code.gif)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="800" height="600" applicationComplete="init()">
<mx:Script>
<![CDATA[
//import RP.Comm;
function init():void {
ExternalInterface.addCallback("testExternalAPI", tst);
}
function test():void{
lblTest.text = ExternalInterface.call("testSentCSharp", "这里可以添加传入的参数");
return;
}
function tst(paramTxt:String):String {
lblTest.text = paramTxt;
return "这是来自Flex的Return";
}
]]>
</mx:Script>
<mx:Button x="117" y="114" label="Button" click="test()"/>
<mx:Label x="19" y="10" text="abc" id="lblTest"/>
</mx:Application>
点下Flex中的button时的情况:
点下C#中的button时的情况: