spark组件中的MX组件为null

时间:2013-08-04 12:49:15

标签: flex4 flex3 flex-spark

我有一个spark Group容器,但它的所有子组件都是MX组件。初始化容器时,我需要对MX组件执行一些操作。我试图将操作放在commitProperties函数中,但子组件在那里仍为空。我尝试将它们移动到childrenCreated函数,但它们仍为空。我可以使用哪些功能来处理组件?感谢。

protected override function commitProperties():void
        {
            var defaultFinishedDate:Date=new Date();
            defaultFinishedDate.date--;
            includeFinishedDateSelector.selectedDate=defaultFinishedDate;
        }

此函数中includeFinishDateSelector为空,因此我遇到运行时错误。它被定义为:

<mx:DateField id="includeFinishedDateSelector" formatString="{GeneralUtils.DATE_FORMAT_STRING}" 
    enabled="{includeFinishedCheckBox.selected}" width="18%"/>

正如我所说,它的容器是一个spark group容器。

3 个答案:

答案 0 :(得分:0)

我原本期望Flex生命周期方法createChildren()是您可以执行操作的地方。但是你只想在超类执行createChildren()之后才做这项工作:

override protected function createChildren():void
{
    super.createChildren();
    // now do your thing
}

另一件事是,在上面显示的commitProperties()方法中,您没有调用超类方法。这是一个很大的禁忌。 Flex框架AF commitProperties()调用createChildren()方法。因此,从理论上讲,使用commitProperties()的方法应该有效 - 您可以返回并在该代码中调用super.commitProperties()并再次使用。

最后,如果这些都不起作用,可能是由于Flex实例化MXML容器中的子对象的方式。因此,一种绝对有效的方法是从Group容器中侦听Flex生命周期事件。当creationComplete调度Group事件时,保证所有子项都存在。

答案 1 :(得分:0)

你也可以尝试将你的代码包装在callLater中,它实际上会将它排队以便在下一次传递时运行。 所以在creationComplete中尝试以下内容:

callLater(function():*{
    var defaultFinishedDate:Date=new Date();
    defaultFinishedDate.date--;
    includeFinishedDateSelector.selectedDate=defaultFinishedDate;
});

它不是很优雅,但它应该有用。

答案 2 :(得分:0)

感谢任何想要帮助的人。最后,我通过将MX组件的直接容器(父)更改为spark容器(它最初是spark容器中的MX容器)来解决这个问题。

相关问题