从 C# 调用 ActionScript 3 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/472234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:45:03  来源:igfitidea点击:

Calling ActionScript 3 function from C#

c#winformsflashactionscript-3

提问by

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.

我有一个嵌入在 Windows 窗体中的 Flash 电影(使用 Visual Studio 8 中包含的组件“Shockwave Flash 对象”)。该 Flash 电影是使用 Flash CS4 创建的,并使用 ActionScript 3。

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

是否可以使用 C# 在返回值的 Flash 电影中调用 ActionScript 函数?

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

另外,我的 Flash 电影是否可以在主应用程序中调用返回值的 C# 函数?

回答by James Hay

ExternalInterface.addCallback(functionName:String, closure:Function):void registers an actionscript method to be callable from it's container.

ExternalInterface.addCallback(functionName:String,closure:Function):void 注册一个可从其容器调用的动作脚本方法。

I'm not sure if it would be able to return a value. My thoughts are that this wouldn't work. If that was the case you could just do another ExternalInterface.call which sends the container the information you want as an argument.

我不确定它是否能够返回一个值。我的想法是这行不通。如果是这种情况,您可以执行另一个 ExternalInterface.call,它将您想要的信息作为参数发送给容器。

I don't know how to actually do the calling from C# but i have seen it done by one of my colleagues for a flash installation project we were working on so i know it's possible... some how!

我不知道如何从 C# 实际调用,但我看到我的一位同事为我们正在进行的 Flash 安装项目完成了它,所以我知道这是可能的...一些方法!

回答by Vojislav Stojkovic

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

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

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

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:

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

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

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

假设您的文本框 (textBox1) 包含文本“World”,在执行上面的代码时,它将包含文本“Hello, World”。

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:

如果您想从 Flash 调用 C# 代码,情况类似:您必须为 Flash 播放器的 FlashCall 事件定义一个事件处理程序。然后,您将使用来自 ActionScript 的以下类型的调用:

ExternalInterface.call("MyCSharpFunction", 17);

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:

这将使 Flash 播放器引发 FlashCall 事件并调用您的事件处理程序。您的处理程序接收的事件参数有一个名为“request”的公共字段,其类型为字符串。请求字段包含描述从 Flash 发出的调用的 XML。对于上面使用的示例,它看起来像这样:

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

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:

如果您希望返回一个值,您在 FlashCall 事件处理程序中要做的就是在事件处理程序返回之前调用 Flash 播放器的 SetReturnValue 方法,将带有描述返回值的 XML 的字符串传递给它,例如:

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

回答by Mrugesh

Please see the given below link you will find proper solutions for your question. your requirement will fulfill. Click Her to get Solutions

请参阅下面给出的链接,您将找到适合您问题的解决方案。您的要求将满足。 点击她获取解决方案