What is the hostComponent?

前端 未结 1 1053
星月不相逢
星月不相逢 2021-01-27 05:00

Im skinning a progressBar in Flex, and after reading a bit about it, I see that there is something called hostComponent.

Adobe site says:

\"The host com         


        
相关标签:
1条回答
  • 2021-01-27 05:43

    When you create custom components in the Spark architecture, you usually split them up into two parts:

    • an ActionScript class that contains the core functionality of the custom component. This class will usually extend SkinnableComponent or SkinnableContainer
    • an MXML skin class which is loosely associated with that ActionScript class and contains only the visual presentation of the component. This class should contain no real functionality and it should be trivial to substitute it with another skin.

    The first of these two classes is referred to as the host component from the skin's point of view.

    A simple example

    Let's create a very simple panel by extending SkinnableContainer:

    public class MyPanel extends SkinnableContainer {
    
        [Bindable]
        public var title:String;
    
    }
    

    As you can see, I made a property 'title' which we want to use to display a title in the Panel. Now let's create a skin that uses this property:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark">
    
        <fx:Metadata>
            [HostComponent("path.to.MyPanel")]
        </fx:Metadata>
    
        <!-- graphics for title bar go here -->
        <s:Label text="{hostComponent.title}" top="5" left="5" />
    
        <!-- graphics for panel content go here -->
        <s:Group id="contentGroup" top="30" bottom="0" left="0" right="0" />
    
    </s:Skin>
    

    The hostcomponent is defined in the 'metadata' block and you see that we can use it to bind its properties into our visual representation. The 'contentGroup' is there because it is required by SkinnableContainer; this is were all the elements will go that you put inside the custom panel. So here's how to use it:

    <myComps:MyPanel title="Panel title" skinClass="path.to.skins.MyPanelSkin">
        <s:Label text="Hello Panel" />
        <!--everything in here goes into the 'contentGroup'-->
    </myComps:MyPanel>
    
    0 讨论(0)
提交回复
热议问题