我有以下MXML:
<mx:Script>
var someBoolean:Boolean = determineSomeCondition();
</mx:Script>
....
<foo:MyComponent somePropertyExpectingIDataRenderer="{
someBoolean
? new Component1ThatImplementsIDataRenderer()
: new Component2ThatImplementsIDataRenderer()
}">
</foo:MyComponent>
我还覆盖了createChildren()函数:
override protected function createChildren():void {
super.createChildren();
//do something with somePropertyExpectingIDataRenderer
}
我的问题是:在评估波浪线括号逻辑之前调用createChildren(),因此在createChildren()中,somePropertyExpectingIDataRenderer为null。
但是如果我通过这样的MXML传递组件:
<foo:MyComponent>
<bar:somePropertyExpectingIDataRenderer>
<baz:Component1ThatImplementsIDataRenderer/>
</bar:somePropertyExpectingIDataRenderer>
</foo:MyComponent>
然后,当调用createChildren()时,该相同的属性不为null。这应该发生,如果是这样,我应该考虑哪些其他解决方法?
答案 0 :(得分:1)
您必须等到组件完成第一个失效阶段才能访问MXML中设置的默认值。这是在调用createChildren()之后,当调度组件的initialize事件时发生的。
我将如何做到这一点:
public function set myProperty(value:IDataRenderer):void
{
if (_myProperty != value)
{
myPropertyChanged = true;
_myPropert = value;
invalidateDisplayList();
}
}
protected override function updateDisplayList(unscaledWidth:Number,
unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
...
if (myPropertyChanged )
doWhateverYouNeedToDo();
}
(当然,这个例子假设更改你的属性需要重绘)