Actionscript 3和mxml ...在等待事件时阻塞

时间:2021-06-16 00:02:58

I am writing a mxml component

我正在写一个mxml组件

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  preinitialize="onPreInitialize();"  "creationComplete()">


<mx:Script>
<![CDATA[
    private function onPreInitialize():void
    {
        addEventListener( "RemoteResourceLoaded", remoteResourceLoaded );
        loadARemoteResource();  
    }
]]>
</mx:Script>

I have some mxml tags in my component that reference variables from the remote resource. This throws null reference errors because flex tries to load all the mxml components before the remote resource has been loaded. I would love it if I could make flex wait in its pre-initialize state, and finish loading the resources before it moved on to initializing all the child components. Any ideas?

我的组件中有一些mxml标签,用于引用远程资源中的变量。这会引发空引用错误,因为flex会在加载远程资源之前尝试加载所有mxml组件。如果我可以在预初始化状态下进行flex等待,并在继续初始化所有子组件之前完成加载资源,我会很高兴。有任何想法吗?

2 个解决方案

#1


You have to chain your methods in a specific manner, you can easily do that in a following way (this code is not tested):

您必须以特定方式链接您的方法,您可以通过以下方式轻松地执行此操作(此代码未经过测试):

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    preinitialize="onPreInitialize();"
    creationComplete="onCreationComplete()">
    <mx:Script>
        <![CDATA[

            private function onPreInitialize():void {
                addEventListener( "RemoteResourceLoaded", remoteResourceLoaded );
                loadARemoteResource();  
            }
            private function loadRemoteResource():void {
                // ...
            }

            private var _created:Boolean = false;

            private function onCreationComplete():void {
                // whatever you have here move it to the runTheApp() method...
                _created = true;
                runTheApp();
            }

            private var _resourcesReady:Boolean = false;

            private function remoteResourceLoaded(event:Event):void {
                // process your resources...
                _resourcesReady = true;
                runTheApp();
            }

            // this method will be called once the app is created
            // and once when your resources are loaded
            //
            // 1:
            // if app is created before resources are loaded its body
            // is not going to be executed as _resourcesReady flag is false
            // when resources are loaded it will then be called again
            // and the body will be executed
            //
            // 2:
            // if the resources are loaded before the app is created
            // (during debugging?) it's gonna be called once but the
            // _created flag is still false so the body is not processed
            // when creationComplete fires both _created is set to true
            // and method is called again, both conditions are true
            // and the body gets executed

            private function runTheApp():void {
                if ( _resourcesReady && _created ) {
                    // now the app is fully created and resources are loaded
                }
            }

        ]]>
    </mx:Script>
</mx:Application>

This shows the generic idea but I think it answers your question. It is generally the matter of waiting for the resource if it takes a long time to load and process creationComplete correctly if resource is loaded before creationComplete fires.

这显示了一般的想法,但我认为它回答了你的问题。如果在createComplete触发之前加载资源,则需要很长时间来加载和处理creationComplete,这通常是等待资源的问题。

Hope this helps.

希望这可以帮助。

#2


Perhaps don't make the loading dependent on remote resources. Your app should gracefully degrade when you have resources that don't load on time, or don't load at all.

也许不要使加载依赖于远程资源。当您的资源无法按时加载或根本不加载时,您的应用程序应正常降级。

Don't load anything external until everything is initialized.

在初始化所有内容之前,不要加载任何外部内容。

#1


You have to chain your methods in a specific manner, you can easily do that in a following way (this code is not tested):

您必须以特定方式链接您的方法,您可以通过以下方式轻松地执行此操作(此代码未经过测试):

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    preinitialize="onPreInitialize();"
    creationComplete="onCreationComplete()">
    <mx:Script>
        <![CDATA[

            private function onPreInitialize():void {
                addEventListener( "RemoteResourceLoaded", remoteResourceLoaded );
                loadARemoteResource();  
            }
            private function loadRemoteResource():void {
                // ...
            }

            private var _created:Boolean = false;

            private function onCreationComplete():void {
                // whatever you have here move it to the runTheApp() method...
                _created = true;
                runTheApp();
            }

            private var _resourcesReady:Boolean = false;

            private function remoteResourceLoaded(event:Event):void {
                // process your resources...
                _resourcesReady = true;
                runTheApp();
            }

            // this method will be called once the app is created
            // and once when your resources are loaded
            //
            // 1:
            // if app is created before resources are loaded its body
            // is not going to be executed as _resourcesReady flag is false
            // when resources are loaded it will then be called again
            // and the body will be executed
            //
            // 2:
            // if the resources are loaded before the app is created
            // (during debugging?) it's gonna be called once but the
            // _created flag is still false so the body is not processed
            // when creationComplete fires both _created is set to true
            // and method is called again, both conditions are true
            // and the body gets executed

            private function runTheApp():void {
                if ( _resourcesReady && _created ) {
                    // now the app is fully created and resources are loaded
                }
            }

        ]]>
    </mx:Script>
</mx:Application>

This shows the generic idea but I think it answers your question. It is generally the matter of waiting for the resource if it takes a long time to load and process creationComplete correctly if resource is loaded before creationComplete fires.

这显示了一般的想法,但我认为它回答了你的问题。如果在createComplete触发之前加载资源,则需要很长时间来加载和处理creationComplete,这通常是等待资源的问题。

Hope this helps.

希望这可以帮助。

#2


Perhaps don't make the loading dependent on remote resources. Your app should gracefully degrade when you have resources that don't load on time, or don't load at all.

也许不要使加载依赖于远程资源。当您的资源无法按时加载或根本不加载时,您的应用程序应正常降级。

Don't load anything external until everything is initialized.

在初始化所有内容之前,不要加载任何外部内容。