带有 AxShockwaveFlash 的 C#/ActionScript [英] C# / ActionScript with AxShockwaveFlash
问题描述
我是 ActionScript 的新手,正在尝试在 AS 和 C# 之间进行通信.我发现的每个教程/解释似乎都完全相同,但我根本无法让它发挥作用.callFunction
抛出一个 COM 异常(E_FAIL"),当我尝试从 AS 调用 ExternalInterface.call()
时,它似乎从来没有.
I am new to ActionScript and am trying to communicate between AS and C#. Every tutorial/explanation I find seems to be exactly the same, but I simply cannot get it to work. callFunction
throws a COM exception ("E_FAIL"), and when I try calling ExternalInterface.call()
from the AS, it never seems to.
我已经让它在 JavaScript/HTML 中工作,但我对 C# 没有想法;我怀疑我做错了什么/在我的 AS 中是不允许的.我使用 Adobe Flex 4.6 和 mxmlc 编译 AS 文件.
I've gotten this to work in JavaScript/HTML, but I'm out of ideas for C#; I suspect I'm doing something wrong/not allowed in my AS. I compile the AS file with Adobe Flex 4.6 and mxmlc.
只是为了澄清,这段代码只显示了从 AS 调用 C# 函数的测试,因为它似乎不太容易出错(不需要 xml 参数处理).另外,我在我的 AS 中使用 while(true) ExternalInterface.call("someFct", "Hello world");
循环运行了一个测试,我的调试过程的 CPU 使用率基本上是与 AS 中没有指令相同 (~0.3%).所以看起来代码根本没有执行.
Just to clarify, this code shows only the test with calling a C# function from AS since it seems less error-prone (no xml argument handling needed). Also, I've run a test with a while(true) ExternalInterface.call("someFct", "Hello world");
loop in my AS and my CPU usage for the debug process is basically the same as with no instructions in the AS (~0.3%). So it seems the code doesn't execute at all.
这是我的 AS 文件:
Here is my AS file:
package
{
import flash.external.ExternalInterface;
public class Test
{
public function Test()
{
ExternalInterface.call("someFct", "Hello world");
}
}
}
在 C# 中,我在 WinForms 中有一个 AxShockwaveFlash 控件:
And in C# I have an AxShockwaveFlash control in WinForms:
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.axShockwaveFlash1 = new AxShockwaveFlashObjects.AxShockwaveFlash();
((System.ComponentModel.ISupportInitialize)(this.axShockwaveFlash1)).BeginInit();
this.SuspendLayout();
//
// axShockwaveFlash1
//
this.axShockwaveFlash1.Enabled = true;
this.axShockwaveFlash1.Location = new System.Drawing.Point(104, 67);
this.axShockwaveFlash1.Name = "axShockwaveFlash1";
this.axShockwaveFlash1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axShockwaveFlash1.OcxState")));
this.axShockwaveFlash1.Size = new System.Drawing.Size(574, 314);
this.axShockwaveFlash1.TabIndex = 0;
this.axShockwaveFlash1.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(this.recv);
this.Click += new System.EventHandler(this.Form1_Click);
//...
((System.ComponentModel.ISupportInitialize)(this.axShockwaveFlash1)).EndInit();
this.ResumeLayout(false);
}
初始化 SWF 对象并接收其调用:
Initializing the SWF object and receiving its call:
private void Form1_Click(object sender, EventArgs e)
{
axShockwaveFlash1.LoadMovie(0, @"mypath\test.swf");
}
//Never executes
public void recv(object sender, _IShockwaveFlashEvents_FlashCallEvent e)
{
string s = e.request;
}
推荐答案
我发现了问题.
1) 正如我所怀疑的,COM 对象需要某种图形初始化才能工作.让你的主类扩展 Sprite
可以解决这个问题.
1) As I suspected, the COM object needs some kind of graphic initialization in order to work at all. Making your main class extend Sprite
solves this.
2) 您显然不能在主类的构造函数中直接使用 ExternalInterface.call()
或 ExternalInterface.setCallback()
.在主类的构造函数中创建一个新类的实例并在其构造函数或任何其他函数中调用 ExternalInterface
可以解决这个问题.
2) You apparently can't use ExternalInterface.call()
or ExternalInterface.setCallback()
in the main class' constructor directly. Creating an instance of a new class in the main class' constructor and making the ExternalInterface
call in its constructor or any other function solves this.
Test.as:
package
{
import flash.display.*;
public class Test extends Sprite
{
public function Test()
{
super();
var x : Test1 = new Test1();
}
}
}
和Test1.as:
package
{
import flash.external.ExternalInterface;
public class Test1
{
public function Test1()
{
ExternalInterface.call("recv", "Hello world");
}
}
}
我也不知道为什么有人会对我的问题投反对票...
I also have no idea why someone would downvote my question...
这篇关于带有 AxShockwaveFlash 的 C#/ActionScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!