一般情况下,当我们写了一个继承自flex组件并往里面添加了子组件,然后想用标签等方式添加子组件时候报错了,那如何解决这一的问题,自定义组件代码如下:

<?xml version="1.0" encoding="utf-8"?>

<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" 

  width="400" height="300">

<mx:Metadata>

[DefaultProperty("children")]

</mx:Metadata>

<mx:Script>

<![CDATA[

private var _children:Array = [];

private var _childrenChanged:Boolean = false;

public function set children(value:*):void

{

if(value is DisplayObject) 

_children = [value];

else

_children = value;

_childrenChanged = true;

invalidateProperties();

}

public function get children():Array

{

return _children;

}

protected override function commitProperties():void

{

super.commitProperties();

if(_childrenChanged)

{

for each(var child:DisplayObject in _children)

{

contentBox.addChild(child);

}

}

}

]]>

</mx:Script>

<mx:Canvas id="contentBox"

  width="100%"

  height="100%"/>

</mx:Canvas>