Monitoring StyleManager progress when loading external assets: using the IEventDispatcher class

Tuesday, 18 November 2008 12:33 by shawny

When loading external stylesheets, often the stylesheet isn't finished loading by the time the application is ready to use it.  This causes some rather abrupt null reference errors that are quite annoying.  To head this problem off, I recommend monitoring the progress of the stylesheet load.  The problem is that the StyleManager.loadStyleDeclarations() method doesn't dispatch any events on its own, and there is not an event that is directly attached to the StyleManager.  However, the Flex documentation notes that the StyleManager.loadStyleDeclaration() method actually returns an object of type IEventDispatcher... this we can use.

Reading the documentation shows us that we can create an object of type IEventDispatcher and set it equal to the loadStyleDeclaration() method and then monitor the event.

private function init():void
    {
      var styleEvent:IEventDispatcher = StyleManager.loadStyleDeclaration("myExternalStyle.swf");
      styleEvent.addEventListener(StyleEvent.PROGRESS, progressHandler);
      styleEvent.addEventListener(StyleEvent.COMPLETE, completeHandler);
      styleEvent.addEventListener(StyleEvent.ERROR, errorHandler);
}

By monitoring the StyleManager progress, we can determine when we want to continue with the application. In the case when you are relying on an external style, moving directly to the applicationComplete event can cause errors later on.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading