如何在应用程序初始化之前在 Flex 中预加载文件 [英] How to preload a file in Flex before the application initializes

查看:19
本文介绍了如何在应用程序初始化之前在 Flex 中预加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:需要在运行时加载一个 XML 配置文件,并在应用程序的 createChildren() 被调用时准备就绪.最迟,因为需要配置值来正确初始化子组件.最好,我希望在创建应用程序之前完成配置加载.简而言之,我想这样做:

Problem: An XML configuration file needs to be loaded at runtime and be ready when the application's createChildren() gets called. At the latest, because configuration values are needed to properly initialize child components. Preferably, I would like the configuration loading be complete before the application even gets created. In short, I want to do this:

  1. 加载配置,然后
  2. 使用加载的配置初始化应用程序.

我创建了一个自定义预加载器来帮助解决这个问题.但事实证明,应用程序的 createChildren() 方法在预加载期间已经被调用,此时还不能保证加载配置.也就是说,在自定义预加载器调度 COMPLETE 事件之前.

I created a custom preloader to help solve this. But as it turns out, the application's createChildren() method already gets called during preloading, when the configuration is not yet guaranteed to be loaded. That is, before the custom preloader dispatches the COMPLETE event.

提前感谢您的帮助.

推荐答案

我找到了问题的解决方案.关键是捕获预加载器的 FlexEvent.INIT_PROGRESS 事件,将其排队并停止其传播,直到配置完全加载.这有效地阻止了框架继续进行应用程序初始化.配置加载后,重新调度排队事件,让框架完成预加载阶段.下面的示例代码(仅相关部分):

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();
    }
}

在应用程序中使用它:

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

这篇关于如何在应用程序初始化之前在 Flex 中预加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆