调用ActionScript 3函数从C#函数、ActionScript

2023-09-09 21:35:50 作者:硪们牵手吧

我有一个Flash影片内嵌在Windows窗体(使用冲击波Flash对象随Visual Studio 8的组件),Flash影片中使用Flash CS4中创建并使用ActionScript 3的。

I have a Flash movie embeded in a Windows Form (using the component "Shockwave Flash Object included with Visual Studio 8). The Flash movie was created with Flash CS4 and uses ActionScript 3.

是否可以使用C#调用ActionScript功能的Flash影片返回一个值?

Is it possible to use C# to call an ActionScript function in the Flash movie that returns a value?

此外,有可能是我的Flash影片调用C#函数返回一个值的主应用程序?

Also, is it possible for my Flash movie to call a C# function in the main application that returns a value?

推荐答案

为了使您的Flash播放器的主机ActionScript函数调用,您必须使用ExternalInterface.addCallback函数中,例如:

In order to make an ActionScript function callable from your Flash player's host, you have to use the ExternalInterface.addCallback function, for example:

ExternalInterface.addCallback("testCallback", function (text : String) : String
{
    var helloText : String = "Hello, " + text;
    myTextField.text = helloText;
    return helloText;
});

为了从您的Windows调用这个函数在C#窗体应用程序,您必须使用由Flash播放器组件公开的CallFunction方法。该方法有一个字符串参数,它必须包含描述调用的XML;它返回其包含描述的返回值的XML字符串。使用上面的例子,这将是一种调用testCallback函数:

In order to call this function from your Windows Forms application in C#, you have to use the CallFunction method exposed by the Flash player component. The method has one string argument, which must contain an XML that describes the call; it returns a string which contains an XML that describes the return value. Using the example above, this would be way to call the testCallback function:

textBox1.Text = flash.CallFunction("<invoke name=\"testCallback\" returntype=\"xml\"><arguments><string>" + textBox1.Text + "</string></arguments></invoke>");

假设你的文本框(textBox1的)包含文本世界,当执行code以上,将包含文本你好,世界。

Supposing that your text box (textBox1) contained the text "World", upon executing the code above it would contain the text "Hello, World".

如果您想从Flash调用C#code,这个故事是相似的:你必须定义一个事件处理您的Flash播放器的的FlashCall事件。然后你会使用以下呼叫类型的动作:

If you want to call C# code from Flash, the story is similar: you have to define an event handler for your Flash player's FlashCall event. Then you would use a following type of call from ActionScript:

ExternalInterface.call("MyCSharpFunction", 17);

这将使Flash播放器提升的FlashCall事件并调用事件处理程序。该事件参数的处理程序接收具有一个公共字段称为请求,其类型为字符串。请求字段包含描述呼叫从Flash制作的XML。对于上面所使用的例子,它是这样的:

This would make the Flash player raise the FlashCall event and call your event handler. The event argument your handler receives has a public field called "request", whose type is string. The request field contains an XML that describes the call made from Flash. For the example used above, it would look like this:

<invoke name="MyCSharpFunction" returntype="xml"><arguments><number>17</number></arguments></invoke>

如果你想返回一个值,你必须做你的FlashCall事件处理的事件处理程序返回之前调用Flash播放器的SetReturnValue方法,传递给它的字符串描述返回值的XML,如

Should you wish to return a value, all you would have to do in your FlashCall event handler is call the Flash player's SetReturnValue method before the event handler returns, passing it the string with the XML that describes the return value, such as:

<string>Works like a charm!</string>