How to preload a file in Flex before the application initializes

久未见 提交于 2019-12-04 16:01:20

I found a solution to the problem. The key was to catch the preloader's FlexEvent.INIT_PROGRESS event, queue it, and stop its propagation until the configuration is fully loaded. This effectively stops the framework to proceed with application initialization. After the configuration loads, redispatch the queued events, letting the framework finish the preloading phase. Example code below (only relevant pieces):

public class PreloaderDisplay extends Sprite implements IPreloaderDisplay {
    // mx.preloaders.IPreloaderDisplay interface
    public function set preloader(preloader:Sprite):void {
        // max priority to ensure we catch this event first
        preloader.addEventListener(FlexEvent.INIT_PROGRESS, onInitProgress, false, int.MAX_VALUE);
        startLoadingConfiguration();
    }
    private function onInitProgress(e:FlexEvent):void {
        if (isConfigurationLoading) {
            queuePreloaderEvent(e);
            e.stopImmediatePropagation();
        }
    }
    private function onConfigurationLoaded():void {
        dispatchQueuedPreloaderEvents();
    }
}

To use it in the application:

<mx:Application preloader="the.package.of.PreloaderDisplay">

The simplest way (I think) is to create a 'holder' canvas that will create the applications content after the context file is loaded, ie:

(psuedo code)

Application.mxml:

<mx:Canvas>
   <mx:Script>
      public function init():void{
          loadXML();
      }

      public function handleXMLLoaded():void{
          this.addChild(myApplicationContent);
      }
   </mx:Script>
</mx:Canvas>

MyApplicationContent.mxml

<mx:Canvas>
<!-- contains all your components etc -->
</mx:Canvas>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!