2014-08-30 42 views
2

我在編寫一個簡單的命令行C#程序,它在編譯的Flash ActionScript 3.0中調用一個函數,然後顯示字符串輸出。在我可以使用的SWF文件中已經註冊了一個回調函數。簡單的C#Flash回調示例

一些研究(mainly following the example here)之後,這是我到目前爲止有:(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); 
     } 
    } 
} 

編譯和運行此給出的 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文件輸出的字符串。這應該是一個更大的程序的一部分,但現在我只想得到一個簡單的例子工作。

+0

我不知道,如果SWF可以用作util控制檯程序。基本上,它只能在Flash播放器中工作。你不能直接啓動swf文件(就像你使用控制檯應用程序一樣)並等待它。 – Ingweland 2014-08-31 11:07:27

回答

1

我的一個項目就是這麼做的,所以我打算與你分享一些代碼片段來幫助你。 首先確保您的項目構建目標是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");