Flex Application里的addChild()
February 15th, 2008
在Flex Application里,是不能直接用addChild添加Sprite,MovieClip等来自flash.display包里的类的。譬如以下代码就会报错:
private function init():void { var sp:Sprite = new Sprite(); addChild(sp);}
private function init():void { var sp:Sprite = new Sprite(); addChild(sp);}TypeError: Error #1034: 强制转换类型失败:无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent。
这是因为Application的addChild方法并非完全继承自DisplayObjectContainer,
Application→LayoutContainer→Container→UIComponent→FlexSprite→Sprite
→DisplayObjectContainer
而是在Container那里被重写了:
public override function addChild(child:displayObject):displayObject
public override function addChild(child:displayObject):DisplayObject虽然参数child的类型是DisplayObject,但是它必须实现IUIComponent接口(所有Flex组件都实现了这一接口),才能添加。
如果要在Application里添加Sprite,可以先把它装进一个UIComponent,然后再添加这个UIComponent:
import mx.core.UIComponent;private function init():void { var sp:Sprite = new Sprite(); var uc:UIComponent = new UIComponent(); uc.addChild(sp); addChild(uc);}
import mx.core.UIComponent;private function init():void { var sp:Sprite = new Sprite(); var uc:UIComponent = new UIComponent(); uc.addChild(sp); addChild(uc);}
posted by mybeky at 12:34 am & filed under flex |
One comment to “Flex Application里的addChild()”
再贴一下Container中重写addChild的部分源码
override public function addChild(child:DisplayObject):DisplayObject
{
return addChildAt(child, numChildren);
}
override public function addChildAt(child:DisplayObject,
index:int):DisplayObject
{
var formerParent:DisplayObjectContainer = child.parent;
if (formerParent && !(formerParent is Loader))
formerParent.removeChild(child);
addingChild(child);
// Add the child to either this container or its contentPane.
// The player will dispatch an "added" event from the child
// after it has been added, so all "added" handlers execute here.
if (contentPane)
contentPane.addChildAt(child, index);
else
$addChildAt(child, _firstChildIndex + index);
childAdded(child);
if ((child is UIComponent) && UIComponent(child).isDocument)
BindingManager.setEnabled(child, true);
// Execute bindings to initialize new children and to refresh
// existing children.
if (child is IDeferredInstantiationUIComponent)
IDeferredInstantiationUIComponent(child).executeBindings(true);
return child;
}
override public function addChild(child:DisplayObject):DisplayObject
{
return addChildAt(child, numChildren);
// Throw an RTE if child is not an IUIComponent.
// 在这里将添加过来的displayobject强制转换成了IUIComponent
//错语的原因也应该是这里,
var uiChild:IUIComponent = IUIComponent(child);
// Set the child's virtual parent, nestLevel, document, etc.
super.addingChild(child);
invalidateSize();
invalidateDisplayList();
if (!contentPane)
{
// If this is the first content child, then any chrome
// that already exists is positioned in front of it.
// If other content children already existed, then set the
// depth of this object to be just behind the existing
// content children.
if (_numChildren == 0)
_firstChildIndex = super.numChildren;
// Increment the number of content children.
_numChildren++;
}
if (contentPane && !autoLayout)
{
forceLayout = true;
// weak reference
UIComponentGlobals.layoutManager.addEventListener(
FlexEvent.UPDATE_COMPLETE, layoutCompleteHandler, false, 0, true);
}
}