Later studies found that the custom objcet has not been assigned a value at createChildren. The Code is as follows:
APP:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: mx = "http://www.adobe.com/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 codeThe Code is as follows: package com
{
Import mx. containers. Panel;
Import mx. events. FlexEvent;
Public class CustomPanel extends Panel
{
Public function CustomPanel ()
{
Super ();
This. addEventListener (FlexEvent. PREINITIALIZE, onPreInit );
This. addEventListener (FlexEvent. INITIALIZE, onInit );
This. addEventListener (FlexEvent. CREATION_COMPLETE, onCreationComplete );
This. addEventListener (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]");
Super. createChildren ();
Trace ("CustomPanel [The end of createChildren]");
}
Override protected function measure (): void
{
Trace ("CustomPanel [Begin to measure]");
Super. measure ();
Trace ("CustomPanel [The end of measure]");
}
Override protected function updateDisplayList (unscaledWidth: Number, unscaledHeight: Number): void
{
Trace ("CustomPanel [Begin to updateDisplayList]");
Super. updateDisplayList (unscaledWidth, unscaledHeight );
Trace ("CustomPanel [The end of updateDisplayList]");
}
Override protected function layoutChrome (unscaledWidth: Number, unscaledHeight: Number): void
{
Trace ("CustomPanel [Begin to layoutChrome]");
Super. layoutChrome (unscaledWidth, unscaledHeight );
Trace ("CustomPanel [The end of layoutChrome]");
}
Override protected function commitProperties (): void
{
Trace ("CustomButton [Begin to commitProperties]");
Super. commitProperties ();
Trace ("CustomButton [The end of commitProperties]");
}
}
}
CustomButton:Copy codeThe Code is as follows: package com
{
Import mx. controls. Button;
Import mx. events. FlexEvent;
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 ();
This. addEventListener (FlexEvent. PREINITIALIZE, onPreInit );
This. addEventListener (FlexEvent. INITIALIZE, onInit );
This. addEventListener (FlexEvent. CREATION_COMPLETE, onCreationComplete );
This. addEventListener (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 )");
Super. createChildren ();
Trace ("CustomButton (The end of createChildren )");
}
Override protected function measure (): void
{
Trace ("CustomButton (Begin to measure )");
Super. measure ();
Trace ("CustomButton (The end of measure )");
}
Override protected function updateDisplayList (unscaledWidth: Number, unscaledHeight: Number): void
{
Trace ("CustomButton (Begin to updateDisplayList )");
Super. updateDisplayList (unscaledWidth, unscaledHeight );
Trace ("CustomButton (The end of updateDisplayList )");
}
Override protected function commitProperties (): void
{
Trace ("CustomButton (Begin to commitProperties )");
Super. commitProperties ();
Trace ("CustomButton (The end of commitProperties )");
}
}
}
The final running result is:
CustomPanel [PreInit]
CustomPanel [Begin to createChildren]
CustomButton (Begin to Constructor)
CustomButton (The end of Constructor)
CustomButton (set customString) // The basic variables (String, Number (int, uint), Boolean) are assigned a value before PreInit.
CustomButton (PreInit)
CustomButton (Begin to createChildren)
CustomButton (The end of createChildren)
CustomButton (Init)
CustomButton (set customObject) // The custom object variable can be assigned a value after Init, so it cannot be obtained in createChildren.
CustomPanel [Init] // when a child control exists, the Init event is sent 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] // proves 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 setting basic variables in MXML is different from setting object variables, that is, object variables must be wrapped in braces {}, so I thought, will it be caused by binding, change the APP to the following, and find that the final output result is the same as the above output:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: mx = "http://www.adobe.com/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>
To further determine whether the assignment period is inconsistent due to binding, I conducted the following experiment without assigning values to the object variables:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: mx = "http://www.adobe.com/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 that of 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 fixed, so you must pay attention when using custom object variables in createChildren later. Otherwise, there will be problems such as null pointers.