我在編寫一個簡單的命令行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文件輸出的字符串。這應該是一個更大的程序的一部分,但現在我只想得到一個簡單的例子工作。
我不知道,如果SWF可以用作util控制檯程序。基本上,它只能在Flash播放器中工作。你不能直接啓動swf文件(就像你使用控制檯應用程序一樣)並等待它。 – Ingweland 2014-08-31 11:07:27