Flex 中的外部接口 API

发布于 2024-11-01 17:20:21 字数 1085 浏览 0 评论 0原文

如何解决mxml程序中的以下错误?

错误是

       Loading configuration file /opt/flex/frameworks/flex-config.xml
       /home/tom-j/programs/flex/html/aa.mxml(17):  Error: Access of undefined property addBody.

       ExternalInterface.addCallback("addBody", addBody);

程序是

     <?xml version="1.0" encoding="utf-8"?>
     <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

     <mx:Script>
     <![CDATA[

     import mx.controls.Button;
     import mx.controls.Alert;
     import flash.display.InteractiveObject;
     import flash.display.Sprite;
     import flash.media.*;
     import flash.net.*;
     import flash.external.*;
     import flash.external.ExternalInterface;

     //                "javascript function", flash function
     ExternalInterface.addCallback("addBody", addBody);

     public function addBody():void
     {
       Alert.show("Got input from JS");
     }
         ]]>                
     </mx:Script>
     </mx:Application>

How to resolve the following error in the mxml program?

The error is

       Loading configuration file /opt/flex/frameworks/flex-config.xml
       /home/tom-j/programs/flex/html/aa.mxml(17):  Error: Access of undefined property addBody.

       ExternalInterface.addCallback("addBody", addBody);

Program is

     <?xml version="1.0" encoding="utf-8"?>
     <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

     <mx:Script>
     <![CDATA[

     import mx.controls.Button;
     import mx.controls.Alert;
     import flash.display.InteractiveObject;
     import flash.display.Sprite;
     import flash.media.*;
     import flash.net.*;
     import flash.external.*;
     import flash.external.ExternalInterface;

     //                "javascript function", flash function
     ExternalInterface.addCallback("addBody", addBody);

     public function addBody():void
     {
       Alert.show("Got input from JS");
     }
         ]]>                
     </mx:Script>
     </mx:Application>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆沫 2024-11-08 17:20:21

您应该尝试将此调用放入 onCreationComplete 事件处理程序中:

protected function onCreationComplete(event:FlexEvent):void
{
    //just in case to prevent security exceptions:
    Security.allowDomain("*.yourdomain.com");
    Security.allowDomain("localhost");
    ExternalInterface.addCallback("addBody", addBody);
}

引发的错误可能是因为在处理 addCallback 行时,不存在 addBody 方法。

更新从 html 中的 javascript 调用 addBody 方法,

您必须嵌入 swf ~如下所示:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="your_flash_app" width="1000" height="520"
    codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
    <param name="movie" value="YourFlashApp.swf" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#bee3f6" />
    <param name="allowScriptAccess" value="always" />
    <embed src="YourFlashApp.swf" quality="high" bgcolor="#bee3f6" width="1000" height="520" name="your_flash_app" align="middle" play="true" loop="false"
        quality="high" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer">
    </embed>
</object>

要从 javascript 调用 addBody 方法,首先您必须在那里找到 Flash 应用程序:

//this only works for sure in IE browsers, but there are workarounds 
//to deal with the others.
var flashApp = document.getElementById("your_flash_app");
flashApp.addBody();

You should try putting this call into an onCreationComplete event handler:

protected function onCreationComplete(event:FlexEvent):void
{
    //just in case to prevent security exceptions:
    Security.allowDomain("*.yourdomain.com");
    Security.allowDomain("localhost");
    ExternalInterface.addCallback("addBody", addBody);
}

The raised error could be because when processing the addCallback line, there did not exist the addBody method.

Update calling the addBody method from javascript

in your html you must have the swf embedded ~like this:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="your_flash_app" width="1000" height="520"
    codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
    <param name="movie" value="YourFlashApp.swf" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#bee3f6" />
    <param name="allowScriptAccess" value="always" />
    <embed src="YourFlashApp.swf" quality="high" bgcolor="#bee3f6" width="1000" height="520" name="your_flash_app" align="middle" play="true" loop="false"
        quality="high" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer">
    </embed>
</object>

To call the addBody method from javascript, first you must locate the flash application there:

//this only works for sure in IE browsers, but there are workarounds 
//to deal with the others.
var flashApp = document.getElementById("your_flash_app");
flashApp.addBody();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文