2011-06-07 135 views
5

使用SDK 4.1我能夠從自定義皮膚訪問自定義按鈕組件的自定義屬性。我目前工作的項目需要SDK 4.5,我無法訪問這些屬性。這裏有一個例子:皮膚時訪問hostComponent的自定義屬性 - Flex 4.5,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 

回答

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> 
+0

你是鑽石。工作過一種享受。 – Trist 2011-06-08 07:41:07

+1

使用''也可以消除編譯錯誤。但是,我發現這兩個解決方案都不顯示我在按鈕欄中設置的圖像(而'保留了這個圖像,不知道爲什麼)。 – ggkmath 2013-07-26 01:35:33

+0

爲什麼?這裏沒有原因。如果我需要'SparkButtonSkin'或什麼的話會怎麼樣?我們可以得到更多的澄清?我知道答案是古老的,但這就是我們在這裏做的不是嗎? – deltree 2016-06-08 19:05:12

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> 
+0

如果我添加這個轉換,編譯錯誤消失了,但被警告「數據綁定將無法檢測到對hostComponent的分配」替換。我可以運行這個警告,但是我從hostComponent屬性獲得的值是無效 – Tony 2012-05-25 16:30:49

+0

非常感謝您的回答Tommy RIAstar對我無效,因爲我需要SparkButtonSkin以某種方式顯示圖像。 – ggkmath 2013-07-26 01:43:11