0

我有弹性应用程序(swf 文件)。有谁知道如何在不使用 flashVars 的情况下从 JavaScript 自动填充 flex textInput?它必须在 FireFox 和 IE 中工作。

4

1 回答 1

1

您可以编写一个wrapper.swf并加载main.swf到其中。

MyApp.mxml- Flex项目申请文件

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:HGroup id="hGroup" width="100%" height="100%" 
        horizontalAlign="center" verticalAlign="middle">
        <s:TextInput id="textInput" text=""/>
    </s:HGroup>
</s:Application>

Wrapper.as- ActionScript 项目应用程序文件

package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;

import mx.core.mx_internal;

use namespace mx_internal;

public class Wrapper extends Sprite
{

    private var loader:Loader;

    private var application:Object;

    public function Wrapper()
    {
        loader = new Loader();
        addChild(loader);
        loader.contentLoaderInfo.addEventListener(Event.INIT, loader_initHandler);
        loader.load(new URLRequest("MyApp.swf"), 
            new LoaderContext(false, ApplicationDomain.currentDomain));
    }

    private function loader_initHandler(event:Event):void
    {
        addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }

    private function enterFrameHandler(event:Event):void
    {
        try
        {
            application = ApplicationDomain.currentDomain.getDefinition(
                "mx.core.FlexGlobals").topLevelApplication;
        }
        catch (error:*)
        {
            return; // app is not ready yet
        }
        if (!application)
            return;

        removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
        if (application.initialized)
            injectText();
        else
            application.addEventListener("creationComplete", application_creationCompleteHandler);
    }

    private function injectText():void
    {
        var textInput:Object = application.getElementAt(0).getElementAt(0);
        if (textInput)
            textInput.text = "Custom value";
    }

    private function application_creationCompleteHandler(event:Event):void
    {
        injectText();
    }

}
}

结果

最后,您可以在包装器中添加任何逻辑,包括ExternalInterface.addCallback.

于 2011-12-10T07:07:49.863 回答