2009-06-11 3 views
0

mxml 구성 요소를 작성 중입니다.Actionscript 3 및 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> 

원격 자원의 변수를 참조하는 일부 구성 요소에 mxml 태그가 있습니다. flex는 원격 리소스가로드되기 전에 모든 mxml 구성 요소를로드하려고하기 때문에 null 참조 오류가 발생합니다. 전 (pre-initialize) 상태에서 flex를 기다릴 수 있고 모든 하위 구성 요소를 초기화하기 전에 리소스를로드하는 것을 끝낼 수 있다면 그것을 좋아할 것입니다. 어떤 아이디어?

답변

0

아마도 로딩을 원격 리소스에 의존하지 마십시오. 제 시간에로드되지 않는 리소스가 있거나 전혀로드하지 않는 경우 앱이 정상적으로 저하되어야합니다.

모든 것이 초기화 될 때까지 외부에 아무것도로드하지 마십시오.

1

당신은 특정한 방식으로 당신의 방법 체인이, 당신은 쉽게 다음과 같은 방법으로 (이 코드를 테스트하지 않음) 것을 수행 할 수 있습니다

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

이것은 일반적인 생각을 보여줍니다하지만 난 그것이 당신의 대답을 생각한다 문제. 일반적으로 creationComplete가 시작되기 전에 리소스가로드 된 경우 creationComplete를로드하고 처리하는 데 시간이 오래 걸리면 일반적으로 리소스를 기다리는 문제가 발생합니다.

희망이 도움이됩니다.

관련 문제