Flex通信篇——Flex和外部应用程序进行通信

news/2024/5/19 19:39:07 标签: flex, string, function, flash, file, exe

Flash To EXE

 

Flex端

  1. Flex创建一个Flex Project命名为Demo1,类型选Web application(runs in Flash Player)
  2. 在Demo1.mxml上添加一个按钮,并未按钮添加点击事件

    private function ButtonClick(event:MouseEvent):void

    {

        //调用外部程序SayHello方法,并传入方法参数"Hunk",输出方法返回值result

        if (!ExternalInterface.available) return ;

        var result:String = ExternalInterface.call("SayHello", "Hunk");

        trace(result);

    }

EXE端

  1. VS2008创建window应用程序项目
  2. 打开默认的窗体,向窗体添加ShockwaveFlashObject控件
  3. 向窗体添加下面代码,为ShockwaveFlashObject控件注册FlashCall事件接收Flash发送过来的请求

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)

    {

    }

}

总结

  1. Flex调用外部应用程序方法(或flash to html)时主要使用ExternalInterface.call方法,详细了解该方法请查阅帮助。该方法会有一个比较严重的问题,当外部应用程序方法执行时间过长(大于60秒)而没有值返回时会报1502错误,所以操作时间大于60秒的方法需要另外的方式来实现,下篇会介绍如何实现异步调用方式解决该问题。
  2. 外部应用程序主要通过注册ShockwaveFlashObject控件的FlashCall事件来接收flash发送过来的请求,请求是xml格式的字符串,详细可查找flex builder的帮助了解。返回值时调用ShockwaveFlashObject控件的SetReturnValue方法。

进阶

理解了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);

        }

    }

}

 

系列索引

Flex通信篇——Flex和外部应用程序进行通信

Flex通信篇——Flex和外部进行异步通信

Flex通信篇——Flex键盘组合键

Flex通信篇——构建企业级HTTP通信层


http://www.niftyadmin.cn/n/1574790.html

相关文章

#初识Express# Express 框架处理前端请求(/:id)

文章涉及到的API&#xff1a;req.params&#xff0c;res.json()&#xff0c;app.get(),app.post(),app.param()。 API讲解 1. app.get()、app.post() 配置客户端路由&#xff08;请求地址&#xff09;。 app.post(/get_json/:id, function (req, res) {// 响应块代码 }) 这里配…

我的报表引擎

技术平台&#xff1a;.net 作品介绍&#xff1a;一个基于微软rdlc的报表引擎&#xff0c;打单是国内中小企必须的日常事务&#xff0c;加上中国式报表的多样性、复杂性&#xff0c;一个良好的报表引擎是每个管理软件都必须的。 交流合作&#xff1a;欢迎技术合作&#xff0c;技…

第21件事 资源支持离不开RACI表

十步法的第九步寻求资源支持。资源主要包括人力资源、物力资源和财力资源。人力资源&#xff0c;即需要多少人&#xff1b;物力资源&#xff0c;即需要多少软硬件设备&#xff1b;财力资源&#xff0c;即需要多少预算。根据产品或项目目标&#xff0c;资源估算时要考虑需要什么…

DataTable还是IList

二进制序列化的情况 在远程系统中&#xff0c;经常需要传输集合类型的数据结构&#xff0c;DataTable和IList<T>是比较常用的2种集合类型&#xff0c;下面对这2种数据类型的二进制序列化作一个测试 定义一个测试的类 using System; using System.Collections.Generic…

我的进销存(商贸版)

技术平台&#xff1a;.net 作品介绍&#xff1a;基于remoting技术构建&#xff0c;C/S结构的进销存系统&#xff0c;有进货管理&#xff0c;销售管理&#xff0c;库存管理&#xff0c;财务管理4大模块&#xff0c;能满足中小企使用需要。 主界面 单据 查询 报表 服务器 主要功…

【C#】wpf添加gif动图支持

原文:【C#】wpf添加gif动图支持1.nuget里下载XamlAnimatedGif包&#xff0c;然后安装。 2.添加XamlAnimatedGif包的命名空间&#xff1a;xmlns:gif"https://github.com/XamlAnimatedGif/XamlAnimatedGif" 3.开始使用&#xff1a; <Image gif:AnimationBehavior.So…

餐饮类排队市场“戏路狭小”,美味不用等终难成大气候?

近期&#xff0c;美味不用等获得来自口碑、携程领投的4亿元D1轮融资。此次融资之后&#xff0c;其在市场上的估计达到了40亿人民币。纵观美味不用等的融资历史可以发现&#xff0c;其投资方集结了各路行业巨头&#xff0c;如经纬中国、阿里、腾讯、携程、美团点评等。如此强大的…

我的进销存(工贸版)

技术平台&#xff1a;.net 作品介绍&#xff1a;基于remoting技术构建&#xff0c;C/S结构的进销存系统&#xff0c;有进货管理&#xff0c;销售管理&#xff0c;库存管理&#xff0c;财务管理&#xff0c;生产管理5大模块&#xff0c;能满足中小企使用需要。 主界面 单据 查…