Using the Flex 4 StyleManager: Getting Style Declarations

As you may know, in Flex 4 you no longer call the StyleManager class as a singleton (StyleManager.getStyleManager()). Instead, StyleManager implements IStyleManager2 and you can get a reference to the styleManager directly, like this:

var styleManager:IStyleManager2 = FlexGlobals.topLevelApplication.styleManager;

A new gotcha I ran across today, while converting a Flex 3 project to Flex 4, was the way style declarations are referenced. Take my current issue as an example:

var styleSheet:CSSStyleDeclaration=FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("DragManager");

I worked for a while to understand why the stylesheet variable was null, every time. Then I decided to actually inspect the selectors attribute of the stylemanager and realized that the reference to the selectors has now changed! The reference must now include the full package of the class. This makes a lot more sense, considering that Spark added a whole set of new classes with the same name. Now I changed my code to the following:

var styleSheet:CSSStyleDeclaration=FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("mx.managers.DragManager");

Perfect! Now my style delcaration was being set as I expected. I hope this tip helps you.

Tags: Flex, Flash & AS3

2 responses so far ↓

  • 1 Roman // Nov 19, 2012 at 1:28 PM

  • 2 Trinu // Feb 15, 2013 at 8:30 AM


    I also faced the same problem but by seeing your post i got the solution.
    Thanks a lot

Leave a Comment

Leave this field empty: