如何在应用程序初始化之前在 Flex 中预加载文件

发布于 2024-08-31 13:11:09 字数 339 浏览 3 评论 0原文

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

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

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

感谢您提前提供的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一口甜 2024-09-07 13:11:09

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

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

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">
素手挽清风 2024-09-07 13:11:09

最简单的方法(我认为)是创建一个“持有者”画布,它将在加载上下文文件后创建应用程序内容,即:(

伪代码)

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文