Flex端
private function ButtonClick(event:MouseEvent):void
{
//调用外部程序SayHello方法,并传入方法参数"Hunk",输出方法返回值result
if (!ExternalInterface.available) return ;
var result:String = ExternalInterface.call("SayHello", "Hunk");
trace(result);
}
EXE端
protected override void OnLoad(EventArgs e)
{
if (m_Init) return;
m_Init = true;
string swfPath = Path.Combine(Application.StartupPath, ConfigurationManager.AppSettings["SwfPath"]);
if (System.IO.File.Exists(swfPath))
{
axShockwaveFlash1.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(axShockwaveFlash1_FlashCall);
//通过配置文件加载Demo1.swf
axShockwaveFlash1.Movie = System.IO.Path.Combine(Application.StartupPath, swfPath);
}
}
private void axShockwaveFlash1_FlashCall(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent e)
{
try
{
//当点击Demo1.swf的按钮时,该方法就会被触发,这里需要解析swf发送过来的xml请求信息
//e.request格式
//<invoke name="SayHello" returntype="xml"><arguments><string>Hunk</string></arguments></invoke>
//根据e.request内容执行相关操作
//操作处理完成后需要返回值到swf,这时需要调用ShockwaveFlashObject控件的SetReturnValue方法
//返回Hello!给Demo1.swf
axShockwaveFlash1.SetReturnValue("<string>Hello!</string>");
}
catch (Exception ex)
{
}
}
总结
进阶
理解了Flex To EXE的原理后就可以设计一些比较有用的方法让EXE来完成Flex所不能完成的操作(不用AIR),下面实现一个本地IO的API
package common
{
import flash.external.ExternalInterface;
public class LocalAPI
{
//Singleton static obj
private static var g_Instance:LocalAPI = null;
//获取LocalAPI单件实例
public static function get Instance():LocalAPI
{
if (g_Instance == null)
{
g_Instance = new LocalAPI();
}
return g_Instance;
}
public function LocalAPI()
{
if (g_Instance != null)
throw new Error("Singleton class. Please use Instance static filed.");
}
//写日志
public function Log(message:String):Number
{
return ExternalInterface.call(Command.FLASH_TO_APP_LOG, message);
}
//复制文件
public function CopyFile(srcFilePath:String, destFilePath:String):Number
{
return ExternalInterface.call(Command.FLASH_TO_APP_COPY_FILE, srcFilePath, destFilePath);
}
//删除文件
public function DeleteFile(filePath:String):Number
{
return ExternalInterface.call(Command.FLASH_TO_APP_DELETE_FILE, filePath);
}
//复制文件夹
public function CopyFolder(srcFolderPath:String, destFolderPath:String):Number
{
return ExternalInterface.call(Command.FLASH_TO_APP_COPY_FOLDER, srcFolderPath, destFolderPath);
}
//删除文件夹
public function DeleteFolder(FolderPath:String):Number
{
return ExternalInterface.call(Command.FLASH_TO_APP_DELETE_FOLDER, FolderPath);
}
//写文件
public function WriteFile(filePath:String, content:String, append:Boolean):Number
{
if (append)
return ExternalInterface.call(Command.FLASH_TO_APP_WRITE_FILE, filePath, content, "true");
else
return ExternalInterface.call(Command.FLASH_TO_APP_WRITE_FILE, filePath, content, "false");
}
//读取文件
public function ReadFile(filePath:String):String
{
return ExternalInterface.call(Command.FLASH_TO_APP_READ_FILE, filePath);
}
//获取路径的子文件夹列表
public function GetFolderList(folderPath:String):Array
{
return ExternalInterface.call(Command.FLASH_TO_APP_GET_FOLDER_LIST, folderPath);
}
//获取路径的子文件列表
public function GetFileList(folderPath:String):Array
{
return ExternalInterface.call(Command.FLASH_TO_APP_GET_FILE_LIST, folderPath);
}
//创建新的文件夹
public function CreateDirectory(folderPath:String):Number
{
return ExternalInterface.call(Command.FLASH_TO_APP_CREATE_FOLDER, folderPath);
}
//判断文件是否存在
public function IsFileExist(path:String):Number
{
return ExternalInterface.call(Command.FLASH_TO_APP_EXIST_FILE, path);
}
//判断文件夹是否存在
public function IsFolderExist(path:String):Number
{
return ExternalInterface.call(Command.FLASH_TO_APP_EXIST_FOLDER, path);
}
}
}
系列索引