在Flex中重绘/刷新自定义皮肤

时间:2012-01-12 00:27:43

标签: flex skin

所以我创建了一个自定义切换按钮,以包含属性currentValue:int

这样我就可以做到以下......

<fx:Script>
    protected var currentValue:int = 0;
    protected function changeValue(x:int):void{
        currentValue = x;
    }
</fx:Script>

<MyToggleButton currentValue="{updatedValue}" styleName="MyToggleStyle" click="changeValue(1)" id="first"/>
<MyToggleButton currentValue="{updatedValue}"  styleName="MyToggleStyle" click="changeValue(2)" id="second"/>

此脚本的想法是根据currentValue的变化更改切换按钮的外观。如何每次都刷新我的切换按钮,以便根据“currentValue”的变化更新样式?

P.S。 MyToggleStyle将引用以mxml编写的Skin类,该类将使用getStyle(“currentValue”)来获取对currentValue的更改

1 个答案:

答案 0 :(得分:0)

如果currentValue属性仅影响皮肤图形,并且您有一些预定义的“外观”,那么您应该创建一个具有状态的外观。 (请参阅spark.skins.spark.CheckBoxSkin以获得一个好例子。)

在MyToggleButton中:

public function set currentValue(value:int):void {
   _currentValue = value;
   invalidateSkinState();
}

protected override function getCurrentSkinState():String {
   return _currentValue == 9 ? "someStateName" : "otherState";
}

currentValue发生变化时,这将使您的皮肤状态无效,Flex会自动调用getCurrentSkinState()来确定您皮肤的新状态。然后,您可以在皮肤中使用includeIn,条件属性等来确定其外观。

如果你想在你的皮肤中显示currentValue作为文本,你应该看看皮肤部分。这些基本上是你的组件定义的插槽,然后皮肤“填充”标签等组件。例如,标准火花按钮上的标签是labelDisplay皮肤部件。该组件可以直接在其上设置文本,但实际创建它是由皮肤决定的。

P.S。在示例代码中,您还需要[Bindable]上的currentValue

相关问题