How to have constants in Flex CSS files
How can I have a constant in a Flex application which I could apply at several places in a Flex CSS file? For example I may have a background color which is the same in several UI components and then I would like to have this color set in only one place and reused in all style cla开发者_JAVA百科uses. Something like ...
public static const myColor = "#00FF00"
...
component1
{
backgroundColor: myColor
}
component2 {
backgroundColor: myColor
}
This is what I use. Check out the StylesheetMixin class on Snipplr.
This is what it looks like in use:
ColorPalette
package
{
// only make bindable if you want to use in skins
// for example like color="{ColorPalette.crazyColor}"
[Bindable]
/**
* This class holds all of your global colors to apply to skins.
*/
public class ColorPalette
{
// "var", not "const", so you can optionally change them at runtime
public static var crazyColor:uint = 0xff0000;
public static const applicationAccent:uint = 0x1a01dd;
}
}
Stylesheet
@namespace mx "library://ns.adobe.com/flex/mx";
@namespace s "library://ns.adobe.com/flex/spark";
@namespace tlf "library://ns.adobe.com/flashx/textLayout";
mx|Panel
{
color: crazyColor;
backgroundColor: applicationAccent;
}
mx|Button
{
color: crazyColor;
backgroundColor: applicationAccent;
}
Flex 3 equivalent:
Panel
{
color: crazyColor;
backgroundColor: applicationAccent;
}
Button
{
color: crazyColor;
backgroundColor: applicationAccent;
}
Sample Application
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:local="*">
<mx:Script>
<![CDATA[
import ColorPalette;
]]>
</mx:Script>
<!-- simple css -->
<mx:Style source="main.css"/>
<!-- stylesheet palette -->
<local:StylesheetMixin palettes="{[ColorPalette]}"/>
<!-- sample container -->
<mx:Panel id="panel" width="100%" height="100%" title="Stylesheet Palettes!">
<mx:Button label="Button"/>
</mx:Panel>
</mx:Application>
I have palettes for:
- Colors
- Assets
- Effects
- Layout
Works with Flex 3 and 4.
Yeah, wouldn't that be nice? There's no way we have found to do this natively. We've tried commenting around colors, like:
background-color:/**mainBGColor**/#FFFFFF/*mainBGColor*/;
and then doing GREP substitutions, but this hasn't been very satisfactory.
A very good question. Adobe, are you listening?
精彩评论