Last week, I was able to implement this into an application we're currently developing at work. It took me a while to figure it out but I must say, it is really handy. A ViewStack for example will allow switching from one child to another, but as the child of that container gets to be really complicated, it may highly affect your application's performance.
Why???
If no creationPolicy is specified for a container, that container inherits its parent's creationPolicy. If no creationPolicy is specified for the Application, it defaults to ContainerCreationPolicy.AUTO.
This is where Deferred Instantiation can save your day.
I have below a basic example of how you can implement Deferred Instantiation using createComponentFromDescriptor()
createComponentFromDescriptor allows you to create one child at a time.
-
<?xml version="1.0" encoding="utf-8"?>
-
<mx:Application
-
xmlns:mx="http://www.adobe.com/2006/mxml"
-
creationComplete="onCreationComplete()"
-
layout="absolute"
-
xmlns:components="components.*">
-
-
<mx:Script>
-
<![CDATA[
-
-
import mx.core.Container;
-
import mx.core.UIComponentDescriptor;
-
import mx.controls.Button;
-
-
private var vsChildren:Array = new Array();
-
-
private function onCreationComplete():void {
-
for(var i:int = 0; i <vs.childDescriptors.length; i++) {
-
-
var lbl:String = "";
-
var descriptor:UIComponentDescriptor = UIComponentDescriptor(vs.childDescriptors[i]);
-
var item:Button = new Button();
-
-
item.addEventListener(MouseEvent.CLICK, onClick);
-
item.label = descriptor.properties.label;
-
item.data = {index:i};
-
cb.addChild(item);
-
}
-
}
-
-
private function onClick(e:MouseEvent):void {
-
var index:int = e.target.data.index;
-
if(!vsChildren[index]) {
-
vsChildren[index] = vs.createComponentFromDescriptor(vs.childDescriptors[index], true) as Container
-
vs.selectedChild = vsChildren[index];
-
vs.validateNow();
-
}else {
-
vs.selectedChild = vsChildren[index];
-
}
-
}
-
-
]]>
-
</mx:Script>
-
<mx:VBox width="100%" height="100%" paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">
-
<mx:HBox id="cb" width="100%" horizontalAlign="center" verticalAlign="middle"/>
-
<mx:ViewStack id="vs" width="100%" creationPolicy="none" height="100%">
-
<components:Red label="Red" width="100%" height="100%" />
-
<components:Green label="Green" width="100%" height="100%" />
-
<components:Blue label="Blue" width="100%" height="100%" />
-
</mx:ViewStack>
-
</mx:VBox>
-
</mx:Application>
SWF:
Download the source code here



August 8th, 2009 at 4:24 am
[...] http://blog.ashier.com/2008/04/07/deferred-instantiation/ Deixe um comentário [...]