当SkinnableComponent中的所有SkinSpart都可用时会触发什么事件?

时间:2011-02-12 03:18:15

标签: flex

我(慢慢)学习Flex 4并且正在设计一个扩展SkinnableComponent的自定义组件。该组件全部使用ActionScript,基本上如下所示:

package components
{
    import spark.components.supportClasses.SkinnableComponent

    [SkinState("normal")]
    [SkinState("over")] 
    [SkinState("selected")]     
    public class AccountSummary extends SkinnableComponent
    {
        [Bindable]
        public var itemIndex:int = 0;

        [Bindable]
        public var accountName:String = "";

        [Bindable]
        public var accountNumber:String = "";   

        [Bindable]
        public var currentBalance:String = "";  

        [SkinPart(required="true")]
        public var lblAccountName:Label;

        [SkinPart(required="true")]
        public var lblCurrentBalance:Label;

        [SkinPart(required="true")]
        public var lblAccountNumber:Label;

        [SkinPart(required="true")]
        public var lblLastUpdated:Label;

        public function AccountSummary()
        {
            super();

            lblAccountName.text = accountName;
            lblCurrentBalance.text = currentBalance;
            lblAccountNumber.text = "Acc: " + accountNumber;
            lblLastUpdated.text = "Last Updated: "; 
        }

        override protected function getCurrentSkinState():String
        {
            return "normal";
        }
    }
}

我遇到的问题是构造函数中针对各种SkinPart的NULL REFERENCES,因为它们尚未创建。我试图找出何时是访问它们以分配其.text值的最佳时间。我知道我可以覆盖partAdded()并在添加每个部分时添加.text值,但我宁愿只是听一些在它们全部可用时调度的魔术事件。

我不确定它是否重要,但是accountName,accountNumber变量是通过在主机组件上使用带有dataProvider的转发器来分配的。当数据可用于AccountSummary组件时,我不确定这是否也有关 - 可能还需要等待。我能够成功使用FlexEvent.CREATION_COMPLETE将所有.text值分配给SkinParts,但这是正确的事件/最佳实践吗?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

您需要阅读Flex 4 Component LifeCycle,特别是有关component instantiation的文章。构造函数执行时不会创建任何内容。您引用的值(accountName,lblCurrentBalance等等)也不会超出默认状态。

您想在partAdded()方法中设置默认值。将构造函数代码重写为以下内容:

    public function AccountSummary()
    {
        super();

    }

override protected function partAdded(partName : String, instance: Object):void{
 super.partAdded(partName, instance);
 if(instance == lblAccountName){
            lblAccountName.text = accountName;
 else if(instance == lblCurrentBalance){

 } else if (instance == lblCurrentBalance){
   lblCurrentBalance.text = currentBalance;
 } else if (instance == lblAccountNumber){
  lblAccountNumber.text = "Acc: " + accountNumber;
 } else if (instance == lblLastUpdated){
  lblLastUpdated.text = "Last Updated: "; 
 }

}

如果您是新的程序员,您可能需要花一些时间来学习一些基本的编程概念。这个ActionScript 3 Guide可以帮助您入门。如果您有其他语言的经验,但对Flex不熟悉,我强烈建议您花几天时间阅读full flex documentation来帮助您了解它。