如何调用从JavaScript(HTMLLoader中)对象AIR的ActionScript方法?对象、方法、HTMLLoader、JavaScript

2023-09-08 11:28:42 作者:北葵向暖

所以,我有我的 AIR 创建一个应用程序沙盒HTMLLoader对象,只是想打电话给从的 JavaScript的。在Flash中,这是通过可信赖的 ExternalInterface.addCallback()函数来完成。然而,在空气,事情相当多的不同,我只是似乎无法得到它的工作。

So I have an Application Sandbox HTMLLoader object which I create in AIR and simply want to call ActionScript methods from JavaScript. In Flash, this is accomplished through our trusty ExternalInterface.addCallback() function. However in AIR, things are quite a bit different, and I just can't seem to get it to work.

下面是我的项目的简单介绍:

Here is a simplified overview of my project:

我的AIR(动作)主:

My AIR (ActionScript) main:

public class Main extends Sprite {

    public var _as3Var:String = "testing";
    public function as3Function():void
    {
        trace("as3Function called from Javascript");
    }

    public function Main() {
        NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke);
    }

    protected function onInvoke(e:InvokeEvent):void {
        NativeApplication.nativeApplication.removeEventListener(InvokeEvent.INVOKE, onInvoke );
        var app = new App();
        addChild(app);
        app.init(new ExternalContainer(), e.currentDirectory, e.arguments);
    }
}

和我这是怎么创建我的HTMLLoader对象:

And this is how I create my HTMLLoader object:

{
    _html = new HTMLLoader();
    _html.useCache = false;
    _html.runtimeApplicationDomain = ApplicationDomain.currentDomain;
    _html.load(new URLRequest("sandbox/AirRoot.html"));
    _html.width = 800;
    _html.height = 600;
    App.ref.addChild(_html);
}

而在去年,这里是我的JavaScript在我的 AirRoot.html 文件,该文件是试图调用公共方法 as3Function片段()在我的主类中声明:

And at last, here is my snippet of JavaScript in my AirRoot.html file which is trying to call the public method as3Function() declared in my Main class:

Exposed.testAs3 = function()
{
    air.trace("Exposed.testAs3 called");            /* This works fine. */
    air.trace("runtimeVersion:");                   /* This works fine. */
    air.trace(air.NativeApplication.nativeApplication.runtimeVersion);  /* This works fine. */
    air.trace("seeing if I can get to AS3 params...");  /* This works fine. */

    /* This doesn't work - get the following error: TypeError: Value undefined does not allow function calls. */
    air.NativeApplication.nativeApplication.as3Function();
}

我在想什么?

What am I missing?

推荐答案

OK,我会回答我的问题。我保证这不是一个阴谋,以获得更多的声誉分,但我今天的严重混淆,但现在已经找到了合适的答案和文件 - 这通常是主要的问题很多工程师的问题...

OK, I am going to answer my own question. I promise this was not a ploy to gain more reputation points, but I was seriously confused today but have now found the appropriate answers and documentation - which is usually the main problem to many an engineer's question...

总之,答案:

在空气HTMLLoader对象包含一个神奇的属性, HTMLLoader.window ,这是对JavaScript的窗口对象的代理。因此,设置 HTMLLoader.window = AS3Function; 是单向的 - 或与pviously包括我的$ P $为例(假设我设置一个静态属性称为主要它指出,主类):

The AIR HTMLLoader object contains a magical property, HTMLLoader.window, which is a proxy to the JavaScript window object. So setting HTMLLoader.window = AS3Function; is one way - or in relation to my previously included example (assuming I setup a static property called Main which pointed to the Main class):

_html.window.as3Function = Main.as3Function;

和现在的JavaScript我可以叫 as3Function 为:

And now in JavaScript I can just call as3Function as:

<script>
    window.as3Function();
</script>

另一个有趣的属性是JavaScript的window.htmlLoader对象。这是对AS3的HTMLLoader父对象的代理,在我的情况下,_html对象。从此,您可以访问有关从JavaScript的_html对象的事情。

Another interesting property is the JavaScript "window.htmlLoader" object. It is a proxy to the AS3 HTMLLoader parent object, in my case, the _html object. From this you can access things in relation to the _html object from JavaScript.