Search code examples
apache-flexflex-spark

How to get notification if skin changed on spark button


I have a spark button subclass with a custom skinclass, defined as follows:

<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      skinClass="com.company.NvrControlButtonSkin"
      enabled="false">

I have then in the script part of this button subclass a setter for an object, where I register a property listener on that object. In the listener, I need to set a property on the skin itself. However, it happens that the object returned by the skin property is not yet of the type I set on the skinClass. I assume that my property listener is already called before the actual skin is set. I would now like to be notified when the skin property is actually changed, so that I then can set the properties on the skin object that are needed.

Is there anything I can listen to or a method that I can override or something to be notified that the skin is actually set to my custom skin now ?


Solution

  • You're in luck, this is an easy fix. SkinnableComponent (and hence also Button) implements the following two protected methods, which you can override:

    • attachSkin(), called every time a new skin is applied to the component
    • detachSkin(), called when the current skin is removed from the component

    That said, I would try to avoid setting properties directly on the skin, because it creates a tight coupling between host component and skin. Try to update properties by using skin parts.