SoFunction
Updated on 2025-04-08

About Flex Initialization Research

Later research found that it was not that it could not be retrieved, but that when creating Children, the custom objcet has not been assigned yet. It will only be assigned after the init event of the component. The code is as follows:
APP:
Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="/2006/mxml"
    layout="absolute" xmlns:com="com.*">
    <mx:Script>
        <!--[CDATA[
            [Bindable]
            private var o:Object = {};
        ]]-->
    </mx:Script>
    <com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
        <com:CustomButton label="Button"
            customString="customString test" customObject="{o}"/>
    </com:CustomPanel>
</mx:Application>

CustomPanel:
Copy the codeThe code is as follows:

package com
{
    import ;
    import ;

    public class CustomPanel extends Panel
    {
        public function CustomPanel()
        {
            super();
            (,             onPreInit);
            (,             onInit);
            (FlexEvent.CREATION_COMPLETE,         onCreationComplete);
            (FlexEvent.APPLICATION_COMPLETE,     onAppInitComplete);
        }

        //=================================
        // event handler
        //=================================

        private function onPreInit(event:FlexEvent):void
        {
            trace("CustomPanel[ PreInit ]");
        }

        private function onInit(event:FlexEvent):void
        {
            trace("CustomPanel[ Init ]");
        }

        private function onCreationComplete(event:FlexEvent):void
        {
            trace("CustomPanel[ CreationComplete ]");
        }

        private function onAppInitComplete(event:FlexEvent):void
        {
            trace("CustomPanel[ AppInitComplete ]");
        }

        //=================================
        // override function
        //=================================

        override protected function createChildren():void
        {
            trace("CustomPanel[ Begin to createChildren ]");
            ();
            trace("CustomPanel[ The end of createChildren ]");
        }

        override protected function measure():void
        {
            trace("CustomPanel[ Begin to measure ]");
            ();
            trace("CustomPanel[ The end of measure ]");
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            trace("CustomPanel[ Begin to updateDisplayList ]");
            (unscaledWidth, unscaledHeight);
            trace("CustomPanel[ The end of updateDisplayList ]");
        }

        override protected function layoutChrome(unscaledWidth:Number, unscaledHeight:Number):void
        {
            trace("CustomPanel[ Begin to layoutChrome ]");
            (unscaledWidth, unscaledHeight);
            trace("CustomPanel[ The end of layoutChrome ]");
        }

        override protected function commitProperties():void
        {
            trace("CustomButton[ Begin to commitProperties ]");
            ();
            trace("CustomButton[ The end of commitProperties ]");
        }
    }
}

CustomButton:
Copy the codeThe code is as follows:

package com
{
    import ;
    import ;

    public class CustomButton extends Button
    {
        //=================================
        // properties
        //=================================
        private var _customString:String     = "";
        private var _customObject:Object     = null;

        public function get customString():String
        {
            return _customString;
        }
        //string
        public function set customString(value:String):void
        {
            trace("CustomButton( set customString )");
            _customString = value;
        }

        //object
        public function get customObject():Object
        {
            return _customObject;
        }

        public function set customObject(value:Object):void
        {
            trace("CustomButton( set customObject )");
            _customObject = value;
        }

        //=================================
        // Constructor
        //=================================

        public function CustomButton()
        {
            trace("CustomButton( Begin to Constructor )");
            super();
            (,             onPreInit);
            (,             onInit);
            (FlexEvent.CREATION_COMPLETE,         onCreationComplete);
            (FlexEvent.APPLICATION_COMPLETE,     onAppInitComplete);
            trace("CustomButton( The end of Constructor )");
        }

        //=================================
        // event handler
        //=================================

        private function onPreInit(event:FlexEvent):void
        {
            trace("CustomButton( PreInit )");
        }

        private function onInit(event:FlexEvent):void
        {
            trace("CustomButton( Init )");
        }

        private function onCreationComplete(event:FlexEvent):void
        {
            trace("CustomButton( Creation Complete )");
        }

        private function onAppInitComplete(event:FlexEvent):void
        {
            trace("CustomButton( AppInitComplete )");
        }

        //=================================
        // override function
        //=================================

        override protected function createChildren():void
        {
            trace("CustomButton( Begin to createChildren )");
            ();
            trace("CustomButton( The end of createChildren )");
        }

        override protected function measure():void
        {
            trace("CustomButton( Begin to measure )");
            ();
            trace("CustomButton( The end of measure )");
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            trace("CustomButton( Begin to updateDisplayList )");
            (unscaledWidth, unscaledHeight);
            trace("CustomButton( The end of updateDisplayList )");
        }

        override protected function commitProperties():void
        {
            trace("CustomButton( Begin to commitProperties )");
            ();
            trace("CustomButton( The end of commitProperties )");
        }
    }
}

The final result is:
CustomPanel[ PreInit ]
CustomPanel[ Begin to createChildren ]
CustomButton( Begin to Constructor )
CustomButton( The end of Constructor )
CustomButton( set customString ) //Basic variables (String, Number(int, uint), Boolean) are assigned before PreInit
CustomButton( PreInit )
CustomButton( Begin to createChildren )
CustomButton( The end of createChildren )
CustomButton( Init )
CustomButton( set customObject ) //Custom object variables can only be assigned after Init, so they cannot be retrieved in createChildren
CustomPanel[ Init ] //When there is a child control, the Init event is emitted in createChildren
CustomPanel[ The end of createChildren ]
CustomButton( set customObject )
CustomButton[ Begin to commitProperties ]
CustomButton[ The end of commitProperties ]
CustomButton( Begin to commitProperties )
CustomButton( The end of commitProperties )
CustomButton( Begin to measure )
CustomButton( The end of measure )
CustomPanel[ Begin to measure ]
CustomPanel[ The end of measure ]
CustomPanel[ Begin to updateDisplayList ]
CustomPanel[ Begin to layoutChrome ]
CustomPanel[ The end of layoutChrome ] //Prove that layoutChrome is called in updateDisplayList
CustomPanel[ The end of updateDisplayList ]
CustomButton( Begin to updateDisplayList )
CustomButton( The end of updateDisplayList )
CustomButton( Creation Complete )
CustomPanel[ CreationComplete ]
Later I found that there is a certain difference between setting basic variables in MXML and object variables, that is, object variables must be wrapped in braces {}, so I wondered if it was caused by binding, and changed the APP to the following, and found that it was the same as expected, and the final output result was the same as above:
Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="/2006/mxml"
    layout="absolute" xmlns:com="com.*">
    <mx:Script>
        <!--[CDATA[
            [Bindable]
private var o:String = "String test";//Replace the original object with a string
        ]]-->
    </mx:Script>
    <com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
        <com:CustomButton label="Button"
            customString="customString test" customObject="{o}"/>
    </com:CustomPanel>
</mx:Application>

In order to further determine that the assignment period is inconsistent due to binding, I did the following experiment, assigning values ​​to object variables without using binding:
Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="/2006/mxml"
    layout="absolute" xmlns:com="com.*">
    <com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
        <com:CustomButton label="Button"
            customString="customString test">
            <com:customObject>
                <mx:ArrayCollection/>
            </com:customObject>
        </com:CustomButton>
    </com:CustomPanel>
</mx:Application>

The result is:
CustomPanel[ PreInit ]
CustomPanel[ Begin to createChildren ]
CustomButton( Begin to Constructor )
CustomButton( The end of Constructor )
CustomButton( set customString )
CustomButton( set customObject ) //The assignment time is the same as the basic variable
CustomButton( PreInit )
CustomButton( Begin to createChildren )
CustomButton( The end of createChildren )
CustomButton( Init )
CustomPanel[ Init ]
CustomPanel[ The end of createChildren ]
CustomButton[ Begin to commitProperties ]
CustomButton[ The end of commitProperties ]
CustomButton( Begin to commitProperties )
CustomButton( The end of commitProperties )
CustomButton( Begin to measure )
CustomButton( The end of measure )
CustomPanel[ Begin to measure ]
CustomPanel[ The end of measure ]
CustomPanel[ Begin to updateDisplayList ]
CustomPanel[ Begin to layoutChrome ]
CustomPanel[ The end of layoutChrome ]
CustomPanel[ The end of updateDisplayList ]
CustomButton( Begin to updateDisplayList )
CustomButton( The end of updateDisplayList )
CustomButton( Creation Complete )
CustomPanel[ CreationComplete ]
The problem is determined, so you must pay attention when using custom object variables in createChildren in the future, otherwise problems such as null pointers will occur.