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

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

问题描述

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


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

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



感谢您提前给予任何帮助。

解决方案

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

pre preublic $ preloaderDisplay extends Sprite实现IPreloaderDisplay {
// mx.preloaders .ipreloaderDisplay接口
public function set preloader(preloader:Sprite):void {
//确保我们先捕获这个事件的最大优先级
preloader.addEventListener(FlexEvent.INIT_PROGRESS,onInitProgress,false, int.MAX_VALUE的);
startLoadingConfiguration();

private function onInitProgress(e:FlexEvent):void {
if(isConfigurationLoading){
queuePreloaderEvent(e);
e.stopImmediatePropagation();

$ b private函数onConfigurationLoaded():void {
dispatchQueuedPreloaderEvents();




$ b $ p $要在应用程序中使用它:

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


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. load configuration, then
  2. initialize the application using the loaded configuration.

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.

Thanks for any help in advance.

解决方案

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">

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

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