Apache flex 在自定义flex组件中设置内容区域

Apache flex 在自定义flex组件中设置内容区域,apache-flex,Apache Flex,我试图定义扩展mx:VBox的自定义组件的内容区域。该组件具有预定义的页眉和页脚Visual Cube,并且我想在中间设置用于向组件添加子元素的区域。组件的使用方式如下: <custom_component> <mx:button/> </custom_component> <?xml version="1.0" encoding="utf-8"?> <mx:VBox xmlns:mx="http://www.adobe.com/

我试图定义扩展mx:VBox的自定义组件的内容区域。该组件具有预定义的页眉和页脚Visual Cube,并且我想在中间设置用于向组件添加子元素的区域。组件的使用方式如下:

<custom_component>
     <mx:button/>
</custom_component>
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
    <mx:Metadata>
        [DefaultProperty("content")]
    </mx:Metadata>

    <mx:HBox id="headerBox"/>
    <mx:VBox id="contentBox"/>
    <mx:HBox id="footerBox"/>

    <mx:Script>
        <![CDATA[
            import mx.core.UIComponent;
            private var _contentChildren:Array;

            public function set content(c:*) : void {
                // Allow 1 or more children to be specified
                _contentChildren = (c as Array) || [c];
            }

            override protected function createChildren() : void {
                // Call super so contentBox gets created first
                super.createChildren();

                for each (var child:UIComponent in _contentChildren) {
                    contentBox.addChild(child);
                }
            }
        ]]>
    </mx:Script>
</mx:VBox>

如何设置此内容区域?

尝试使用画布?

尝试使用画布?

在自定义组件中,添加DefaultProperty元数据标记:

[DefaultProperty("nameOfDefaultProperty")]
然后还要为该属性定义一个setter:

public function set nameOfDefaultProperty(value:UIComponent):void
{
    if (value != null)
    {
        // add "value" to the display list here
    }
}

在自定义组件中,添加DefaultProperty元数据标记:

[DefaultProperty("nameOfDefaultProperty")]
然后还要为该属性定义一个setter:

public function set nameOfDefaultProperty(value:UIComponent):void
{
    if (value != null)
    {
        // add "value" to the display list here
    }
}

实际上有几个步骤

您的自定义组件需要设置其DefaultProperty元数据,这样子组件就不会与自定义组件本身中的子组件发生冲突。 然后,您需要将它们存放在实例变量中,以便稍后添加到内容区域,因为属性将在创建子组件之前设置。 最后,如果指定了多个子对象,DefaultProperty将获得一个数组对象,而不是一个UIComponent实例。 因此,您的自定义组件将如下所示:

<custom_component>
     <mx:button/>
</custom_component>
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
    <mx:Metadata>
        [DefaultProperty("content")]
    </mx:Metadata>

    <mx:HBox id="headerBox"/>
    <mx:VBox id="contentBox"/>
    <mx:HBox id="footerBox"/>

    <mx:Script>
        <![CDATA[
            import mx.core.UIComponent;
            private var _contentChildren:Array;

            public function set content(c:*) : void {
                // Allow 1 or more children to be specified
                _contentChildren = (c as Array) || [c];
            }

            override protected function createChildren() : void {
                // Call super so contentBox gets created first
                super.createChildren();

                for each (var child:UIComponent in _contentChildren) {
                    contentBox.addChild(child);
                }
            }
        ]]>
    </mx:Script>
</mx:VBox>

实际上有几个步骤

您的自定义组件需要设置其DefaultProperty元数据,这样子组件就不会与自定义组件本身中的子组件发生冲突。 然后,您需要将它们存放在实例变量中,以便稍后添加到内容区域,因为属性将在创建子组件之前设置。 最后,如果指定了多个子对象,DefaultProperty将获得一个数组对象,而不是一个UIComponent实例。 因此,您的自定义组件将如下所示:

<custom_component>
     <mx:button/>
</custom_component>
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
    <mx:Metadata>
        [DefaultProperty("content")]
    </mx:Metadata>

    <mx:HBox id="headerBox"/>
    <mx:VBox id="contentBox"/>
    <mx:HBox id="footerBox"/>

    <mx:Script>
        <![CDATA[
            import mx.core.UIComponent;
            private var _contentChildren:Array;

            public function set content(c:*) : void {
                // Allow 1 or more children to be specified
                _contentChildren = (c as Array) || [c];
            }

            override protected function createChildren() : void {
                // Call super so contentBox gets created first
                super.createChildren();

                for each (var child:UIComponent in _contentChildren) {
                    contentBox.addChild(child);
                }
            }
        ]]>
    </mx:Script>
</mx:VBox>