O'Reilly logo

Programming Flex 2 by Joey Lott, Chafic Kazoun

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Runtime CSS

In Flex 2.0.1 and higher, you can use runtime CSS. Thus far in the chapter, when we've talked about CSS, we've talked exclusively about CSS that gets compiled into the Flex application. Now we'll look at how you can load and apply CSS at runtime, effectively restyling an application at runtime.

Because Flex styles often require embedded resources such as skin assets, classes, and fonts, Flex runtime CSS must be precompiled into .swf files. When using Flex 2.0.1 you can use the mxmlc compiler to compile a .css file into an .swf that includes all assets embedded by the .css file. In order to compile the .css file, simply pass the path to the file as the one compiler argument, as in the following example:

mxmlc styles.css

The preceding example compiles styles.css into a file called styles.swf. You can then load styles.swf into your Flex applications at runtime without having to include the .css or any of the embedded assets in the main Flex application. That means you can update the styles for an application without having to recompile and redeploy the application itself. All you have to do is update the styles file. Furthermore, you can have many styles files that the user can choose from at runtime.

In order to load CSS at runtime, you use the StyleManager.loadStyleDeclarations( ) method. The loadStyleDeclarations( ) method requires at least one parameter--the path to the .swf file containing the CSS:

StyleManager.loadStyleDeclarations("styles.swf");

If you're loading only ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required