C#Flash回调的简单示例

本文关键字:简单 回调 C#Flash | 更新日期: 2023-09-27 18:20:08

我正在尝试编写一个简单的命令行C#程序,该程序调用已编译的Flash ActionScript 3.0中的一个函数,然后显示字符串输出。SWF文件中已经注册了一个回调函数,我可以使用它。

经过一些研究(主要遵循这里的例子),这就是我迄今为止所拥有的:

using AxShockwaveFlashObjects;
using System;
namespace ConsoleProg {
    class ConsoleProg {
        static void Main (string[] args) {
            string path = "container.swf";
            AxShockwaveFlash flash = new AxShockwaveFlash();
            flash.LoadMovie(0, path);
            string result = flash.CallFunction("<invoke name='"api_function'" returntype='"xml'"><arguments></arguments></invoke>");
            Console.WriteLine(result);
        }
    }
}

(SWF文件中的回调函数不接受任何参数,并返回一个短字符串。)

编译和运行此程序会出现以下错误:ActiveX control cannot be instantiated because the current thread is not in a single-threaded apartment.

所以我环顾四周,尝试了两件事:将[STAThread]添加到Main方法中。现在它给出错误

Unhandled Exception: System.Windows.Forms.AxHost+InvalidActiveXStateException: E
xception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was
thrown.

我做的另一件事是将控制台程序分解为一个对象,并在它自己的线程中实例化它,明确指定它的单元状态:

using AxShockwaveFlashObjects;
using System.Threading;
using System;
namespace ConsoleProg {
    class ConsoleProg {
        public string path;
        public ConsoleProg (string path) {
            this.path = path;
        }
        public void LoadFlash() {
            AxShockwaveFlash flash = new AxShockwaveFlash();
            flash.LoadMovie(0, this.path);
            string result = flash.CallFunction("<invoke name='"api_function'" returntype='"xml'"><arguments></arguments></invoke>");
            Console.WriteLine(result);
        }
        static void Main (string[] args) {
            ConsoleProg cp = new ConsoleProg("api_container.swf");
            Thread t = new Thread(cp.LoadFlash);
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
    }
}

这给出了与我上面的另一次尝试相同的错误。

在这一点上我不知所措。我做错了什么?如果有人能给我指一个简单但完整的例子,那将非常有帮助,因为我在网上看到的所有教程都是代码片段,我无法将其组装成有效的东西。

我所需要的程序就是在控制台中打印flash文件输出的字符串。这应该是一个更大程序的一部分,但现在我只想得到一个简单的例子。

C#Flash回调的简单示例

我的一个项目就是这样做的,所以我将与您分享一些代码片段,以帮助您。首先,确保您的项目构建目标是x86。我根本无法让x64或AnyCpu工作。谁知道呢,这可能会解决你所有的问题。

如果没有,请尝试使用我从项目中借来的代码来执行函数调用:

public string InvokeFlashFunction(string functionName, params object[] functionParameters)
{
    //Creates the xml that will be sent to the flash movie
    XmlDocument invokeDocument = new XmlDocument();
    //Creates the invoke element that will be the root element of the xml
    XmlElement rootElement = invokeDocument.CreateElement("invoke");
    //Set both attributes of the invoke element
    rootElement.SetAttribute("name", functionName);
    rootElement.SetAttribute("returnType", "xml");
    //Creates the arguments element
    XmlElement childNode = invokeDocument.CreateElement("arguments");
    //foreach parameter, creates a child node to the arguments element
    foreach (object param in functionParameters)
    {
        XmlElement paramNode = GetParamXml(param, invokeDocument);
        //appends the parameters
        childNode.AppendChild(paramNode);
    }
    //Appends the arguments node
    rootElement.AppendChild(childNode);
    //Call the function
    return _FlashMovie.CallFunction(rootElement.OuterXml);
}

你会这样使用它:

InvokeFlashFunction("api_function", "your arguments here");