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

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

 

  1. [SkinPart(required="false")]           
  2. 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.       

 

在增加或删除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. }  
posted @ 2010-12-28 21:16  小宝马的爸爸-gisera  阅读(558)  评论(2编辑  收藏  举报