在SkinnableComponent中,可以声明SkinPart元标签对Skin进行约束,同时在组件中提供对Skin元素的引用:
[SkinPart(required="false")] public var labelElement:SimpleText;
如果声明了SkinPart并且required="true",则Skin中必须包含该类型灯元素并且具有相同的id:
<s:SimpleText id="labelElement" .../>
SkinnableComponent中还提供了partAdded和partRemoved方法:
//-------------------------------------------------------------------------- // // Methods - Parts // //-------------------------------------------------------------------------- /** * Called when a skin part is added. * You do not call this method directly. * For static parts, Flex calls it automatically when it calls the <code>attachSkin()</code> method. * For dynamic parts, Flex calls it automatically when it calls * the <code>createDynamicPartInstance()</code> method. * * <p>Override this function to attach behavior to the part. * If you want to override behavior on a skin part that is inherited from a base class, * make sure that you do not call the <code>super.partAdded()</code> method. * Otherwise, you should always call the <code>super.partAdded()</code> method.</p> * * @param partname The name of the part. * * @param instance The part. * * @langversion 3.0 * @playerversion Flash 10 * @playerversion AIR 1.5 * @productversion Flex 4 */ protected function partAdded(partName:String, instance:Object):void { } /** * Called when an instance of a skin part is being removed. * You do not call this method directly. * For static parts, Flex calls it automatically when it calls the <code>detachSkin()</code> method. * For dynamic parts, Flex calls it automatically when it calls * the <code>removeDynamicPartInstance()</code> method. * * <p>Override this function to remove behavior from the part.</p> * * @param partname The name of the part. * * @param instance The part. * * @langversion 3.0 * @playerversion Flash 10 * @playerversion AIR 1.5 * @productversion Flex 4 */ protected function partRemoved(partName:String, instance:Object):void { }
在增加或删除SkinPart时会调用这些方法。
通过重写这些方法可以对SkinPart进行额外的操作,比如增加SkinPart时为其添加事件监听:
override protected function partAdded(partName:String, instance:Object) : void { super.partAdded(partName, instance); if (instance == labelElement) labelElement.addEventListener(...); }