Flex4中的皮肤(4):使用SkinPart约束Skin

本文介绍了SkinnableComponent中的SkinPart元标签及其使用方法,包括如何声明SkinPart及其实例化过程。此外,还详细解释了partAdded和partRemoved方法的作用及应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在SkinnableComponent中,可以声明SkinPart元标签对Skin进行约束,同时在组件中提供对Skin元素的引用:

 

  1. [SkinPart(required="false")]           
  2. public var labelElement:SimpleText;  

[SkinPart(required="false")] public var labelElement:SimpleText;

 

如果声明了SkinPart并且required="true",则Skin中必须包含该类型灯元素并且具有相同的id:

<s:SimpleText id="labelElement" .../>

SkinnableComponent中还提供了partAdded和partRemoved方法:

 

  1. //--------------------------------------------------------------------------   
  2.     //   
  3.     //  Methods - Parts   
  4.     //   
  5.     //--------------------------------------------------------------------------   
  6.        
  7.     /**  
  8.      *  Called when a skin part is added.   
  9.      *  You do not call this method directly.   
  10.      *  For static parts, Flex calls it automatically when it calls the <code>attachSkin()</code> method.   
  11.      *  For dynamic parts, Flex calls it automatically when it calls   
  12.      *  the <code>createDynamicPartInstance()</code> method.   
  13.      *  
  14.      *  <p>Override this function to attach behavior to the part.   
  15.      *  If you want to override behavior on a skin part that is inherited from a base class,   
  16.      *  make sure that you do not call the <code>super.partAdded()</code> method.   
  17.      *  Otherwise, you should always call the <code>super.partAdded()</code> method.</p>  
  18.      *  
  19.      *  @param partname The name of the part.  
  20.      *  
  21.      *  @param instance The part.  
  22.      *    
  23.      *  @langversion 3.0  
  24.      *  @playerversion Flash 10  
  25.      *  @playerversion AIR 1.5  
  26.      *  @productversion Flex 4  
  27.      */  
  28.     protected function partAdded(partName:String, instance:Object):void  
  29.     {      
  30.     }   
  31.     /**  
  32.      *  Called when an instance of a skin part is being removed.   
  33.      *  You do not call this method directly.   
  34.      *  For static parts, Flex calls it automatically when it calls the <code>detachSkin()</code> method.   
  35.      *  For dynamic parts, Flex calls it automatically when it calls   
  36.      *  the <code>removeDynamicPartInstance()</code> method.   
  37.      *  
  38.      *  <p>Override this function to remove behavior from the part.</p>  
  39.      *  
  40.      *  @param partname The name of the part.  
  41.      *  
  42.      *  @param instance The part.  
  43.      *    
  44.      *  @langversion 3.0  
  45.      *  @playerversion Flash 10  
  46.      *  @playerversion AIR 1.5  
  47.      *  @productversion Flex 4  
  48.      */  
  49.     protected function partRemoved(partName:String, instance:Object):void  
  50.     {          
  51.     }   
  52.       

//-------------------------------------------------------------------------- // // 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时为其添加事件监听:

 

  1. override protected function partAdded(partName:String, instance:Object) : void  
  2. {   
  3.        super.partAdded(partName, instance);   
  4.        if (instance == labelElement)   
  5.            labelElement.addEventListener(...);   
  6. }  

转载于:https://www.cnblogs.com/gisera/archive/2010/12/28/1919117.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值