Доступ к пользовательскому свойству hostComponent при создании скинов - Flex 4.5, SDK 4.5 - PullRequest
5 голосов
/ 07 июня 2011

Используя 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 ]

8 голосов
/ 07 июня 2011

Просто замените 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>
2 голосов
/ 26 июля 2011

Другой вариант, если вы хотите использовать 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...