设置itemCreationPolicy使页面创建时元素已经创建

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" 
			   creationComplete="init(event)">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			protected function init(event:FlexEvent):void
			{
				this.btnBig.addEventListener(MouseEvent.CLICK,bar);
				this.btnsmall.addEventListener(MouseEvent.CLICK,bar);
			}
			
			private function bar(event:MouseEvent):void{
			}
			
		]]>
	</fx:Script>
	<s:states>
		<s:State name="big"/>
		<s:State name="small"/>
	</s:states>
	<s:Button includeIn="big" id="btnBig"/>
	<s:Button includeIn="small" id="btnsmall"/>
</s:Application>


在这个页面中有两个按钮,分别出现在两个状态中.默认情况下,flex不创建btnsmall按钮,
因此页面的creationComplete方法中访问btnsmall会报空指针异常.
设置creationComplete属性则可以立即创建组件
    <s:Button includeIn="big" id="btnBig" itemCreationPolicy="immediate"/>
<s:Button includeIn="small" id="btnsmall"  itemCreationPolicy="immediate"/>
详细介绍可以看 http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf63611-7ffa.html
的Controlling when to create added children

你可能感兴趣的:(Flex)