簡體   English   中英

我們如何在Flex中使用addCallback函數

[英]How can we use addCallback function in flex

我如何從Java腳本中調用flex函數? 我正在使用下面的代碼在下面的鏈接中定義

ExternalInterface.addCallback( "javascriptfunction", flexfunction);

http://www.switchonthecode.com/tutorials/flex-javascript-basics-using-externalinterface

http://circlecube.com/2010/12/actionscript-as3-javascript-call-flash-to-and-from-javascript/

您應該為您的問題提供更多背景信息。 根據我在教程中閱讀的內容,可能省略了為代碼添加安全性訪問的過程。 更精確地看這里: http : //help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#addCallback%28%29 ,尤其是這里:

SecurityError —包含環境屬於調用代碼無法訪問的安全沙箱。 若要解決此問題,請按照下列步驟操作:在包含HTML頁面的SWF文件的對象標記中,設置以下參數:

param name="allowScriptAccess" value="always" 

In the SWF file, add the following ActionScript:

flash.system.Security.allowDomain(sourceDomain)

希望能幫助到你。

flex 4.5

首先在您的mxml中添加一個addcallback

public function initApp():void {
        ExternalInterface.addCallback("myFlexFunction",myFunc);
}  

現在可以從您的javascript訪問myFlexFunction

使您的index.template.html看起來像這樣

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <title>addCallback() Wrapper</title>
        <script type="text/javascript" src="swfobject.js"></script>
        <script type="text/javascript">
            var swfVersionStr = "0";
            var xiSwfUrlStr = "";
            var flashvars = {};
            var params = {};
            params.quality = "high";
            params.bgcolor = "#ffffff";
            params.allowscriptaccess = "sameDomain";
            var attributes = {};
            attributes.id = "AddCallbackExample";
            attributes.name = "AddCallbackExample";
            attributes.align = "middle";
            swfobject.embedSWF(
                "AddCallbackExample.swf", "flashContent",
                "100%", "100%",
                swfVersionStr, xiSwfUrlStr,
                flashvars, params, attributes);
        </script>
    </head>
    <SCRIPT LANGUAGE="JavaScript">
        function callApp() {
            window.document.title = document.getElementById("newTitle").value;
            var AddCallbackExample = document.getElementById("AddCallbackExample");
            AddCallbackExample.myFlexFunction(window.document.title);
        }
    </SCRIPT>

    <body>
        <form id="f1">
            Enter a new title: <input type="text" size="30" id="newTitle" onchange="callApp()">
        </form>
        <div id="flashContent"/>
   </body>
</html>

和你的Flex應用程序是這樣的

<?xml version="1.0"?>
<!-- wrapper/AddCallbackExample.mxml -->
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="initApp()">
    <fx:Script>
        import flash.external.*;

         public function initApp():void {
            ExternalInterface.addCallback("myFlexFunction",myFunc);
         }  

         public function myFunc(s:String):void {
            l1.text = s;
         }
    </fx:Script>

    <s:Label id="l1"/>

</s:Application>

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM