从预加载器调用应用程序中的函数作为代码

时间:2012-02-29 07:08:30

标签: flex flex4 flex4.5 flexbuilder

我想在调度complete事件之前从actionscript中的自定义预加载器代码调用main flex app中的函数。 此外,我想知道如何从应用程序

回调预加载器代码中的函数

由于

1 个答案:

答案 0 :(得分:0)

因此,在预加载器init方法中,您可以使用以下行来获取应用程序:

        parentApplication = event.currentTarget.loaderInfo.content.application;

我们的预加载器实现了一个名为IPreloader的自定义接口,我们的App实现了一个名为IPreloaderApp的自定义接口,IPreloader定义为:

package com.roundarch.adapt.preloader
{
    import mx.preloaders.IPreloaderDisplay;

    public interface IPreloader extends IPreloaderDisplay
    {
        /**
         * Setting this will update the preloader's percentage bar or graphic.
         */
        function set percentage(value:Number):void;
        function get percentage():Number;

        /**
         * Sets the status (if available) on the preloader.
         */
        function set status(value:String):void;

        /**
         * This will communicate to the preloader that loading of all application
         * properties is complete.
         */
        function loadingComplete():void;

        /**
         * This will tell the preloader that there has been an error loading the application.
         *
         * The preloader will probably want to display this error message in a nice display to
         * the user.
         *
         * @param errorString The error message to display.
         * @param fatal If true, the preloader should act as if the application will not run perhaps
         * by notifying the user that they cannot continue.
         * @return Should return true if the error was properly handled.
         */
        function displayError(errorString:String, fatal:Boolean=false):Boolean;

        /**
         * Returns true if this IPreloader implementation can handle displaying loading
         * errors through the error(...) method. If false, the implementing application will
         * need to notify the user of any errors itself.
         */
        function get canDisplayErrors():Boolean;
    }
}

和IPreloaderApp

package com.roundarch.adapt.preloader
{

    /**
     * Any application which uses an IPreloader as its preloader will be
     * required to implement this interface or throw an error.
     */
    public interface IPreloaderApp
    {
        /**
         * Once the application has loaded and initialized, this method will be called on the
         * application and the preloader will be passed in so the app can make updates to it.
         */
        function preloaderInit(preloader:IPreloader):void;
    }
}

另外需要注意的是,如果应用程序添加到预加载器后面而不是在前面(默认行为),那么您将看不到应用程序弹出的警报,因为它们将位于预加载器后面,因此您我想切换订单。以下是我们用来解决这个问题的一些代码:

    private var removedOnce : Boolean;

    //When the Preloader class instance (this things parent) is removed from it's parent
    //add this to the stage, allows us to dispatch the complete event at the corret time
    //allowing the SystemManager to add the application to the stage and adding this at
    //index 0 to allow pop-ups to show over it.
    private function parentRemoved(event : Event) : void
    {
        if (!removedOnce)
        {
            removedOnce = true;

            stage.addChildAt(this, 0);
            ToolTipManager.enabled = false;
        }
    }

在预加载器初始化处理器中设置parentApplication之后,如本文顶部所示,我们添加一个处理程序来捕获SystemManager删除预加载器并重新添加它(没有可见的闪烁)

        //Lets the system manager know to add the application to the stage
        //this will also remove the preloader for this reason I'm listening for the removal
        //then adding the preloader to the stage again
        parent.addEventListener(Event.REMOVED, parentRemoved);
        dispatchEvent(new Event(Event.COMPLETE));

另外需要注意的是,您不希望使用任何Flex类“污染”预加载器代码,如果您从框架中包含内容,则最终必须在预加载器开始之前将整个框架加载到内存中(可能很重)。