设置外观时访问hostComponent的自定义属性 - Flex 4.5,SDK 4.5

时间:2011-06-07 16:10:39

标签: actionscript-3 properties skinning

使用SDK 4.1我能够从自定义皮肤访问自定义按钮组件的自定义属性。我目前正在工作的项目需要SDK 4.5,我无法访问这些属性。这是一个例子:

自定义按钮组件

<?xml version="1.0" encoding="utf-8"?>
<s:ButtonBase xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx"
          skinClass="components.skins.ButtonIcon_Skin"
          >
    <fx:Declarations>
        <fx:String id="iconCustom" />
    </fx:Declarations>
</s:ButtonBase>

自定义按钮皮肤

<?xml version="1.0" encoding="utf-8"?>
<s:SparkButtonSkin xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
             minWidth="21" minHeight="21" 
             alpha.disabled="0.5">
    <fx:Metadata>[HostComponent("components.ButtonIcon")]</fx:Metadata>

...

    <s:Label id="test" {hostComponent.iconCustom}" 
             horizontalCenter="0" bottom="10" />

</s:SparkButtonSkin>

代码提示显示hostComponent.iconCustom,但随后出现错误:

Access of possibly undefined property iconCustom through a reference with static type spark.components.supportClasses:ButtonBase. ButtonIcon_Skin.mxml

2 个答案:

答案 0 :(得分:8)

只需用常规皮肤替换SparkButtonSkin,你就可以了:

<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark">

    <fx:Metadata>
        [HostComponent("components.ButtonIcon")]
    </fx:Metadata>

    <s:states>
        <s:State name="disabled" />
        <s:State name="down" />
        <s:State name="over" />
        <s:State name="up" />
    </s:states>

    <s:Label text="test {hostComponent.iconCustom}" 
             horizontalCenter="0" bottom="10" />

</s:Skin>

答案 1 :(得分:2)

另一种选择,如果你想使用SparkButtonSkin,只需转换为你的实际hostComponent

(hostComponent as ButtonIcon).iconCustom

或在上下文中:

自定义按钮皮肤

<?xml version="1.0" encoding="utf-8"?>
<s:SparkButtonSkin xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
             minWidth="21" minHeight="21" 
             alpha.disabled="0.5">
    <fx:Metadata>[HostComponent("components.ButtonIcon")]</fx:Metadata>

...

    <s:Label id="{(hostComponent as ButtonIcon).iconCustom}" 
             horizontalCenter="0" bottom="10" />

</s:SparkButtonSkin>