1. 问题:
Flex 4中用Flex 3代码:StyleManager.getStyleDeclaration('Button').setStyle('fontSize',24);
报错如下:
自 4.0 以来已弃用。 请使用 IStyleManager2.getStyleDeclaration on a style manager instance
获取存储指定 CSS 选择器规则的 CSSStyleDeclaration 对象。
2. 分析:
Flex4中不能再调用StyleManager类作为单例模式了。
3.解决:
StyleManager.getStyleManager(null).getStyleDeclaration('mx.controls.Button').setStyle('fontSize',24);
4. 实例代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="initApp(event)">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
mx|ToolTip {
fontSize: 50;
color: #ED1A3D;
backgroundColor: #000000;
}
.myStyle {
color: red;
}
</fx:Style>
<fx:Script>
<![CDATA[
public function initApp(e:Event):void {
import mx.managers.SystemManagerGlobals;
var manager:IStyleManager2 = StyleManager.getStyleManager(null);
var s:CSSStyleDeclaration = manager.getStyleDeclaration("mx.controls.ToolTip");
s.setStyle("fontSize", 50);
s.setStyle("color", 0xED1A3D);
s.setStyle("backgroundColor", 0x0);
//得到CSSStyleDeclaration类的样式并设定"Button"样式
manager.getStyleDeclaration("spark.components.Button"). setStyle("fontSize",24);
//设定一个新的样式名称
manager.getStyleDeclaration(".myStyle").setStyle("color",0xCC66CC);
}
]]>
</fx:Script>
<s:Button y="79" label="第一个按钮" horizontalCenter="0" toolTip="button" />
<s:Label y="147" id="myLabel" text="This is a label" styleName="myStyle" horizontalCenter="0"/>
</s:Application>