myComponent:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
- <mx:Label x="111.5" y="70" text="Welcome to myComponent"/>
- </mx:Panel>
- <?xml version="1.0" encoding="utf-8"?>
- <MyComponent xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml" >
- <mx:Button />
- </MyComponent>
也即是flex 3不支持子類直接添加新的control 。 必須通過 重寫childrenCreated方法來實現。新的代碼如下:
MyComponent:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300" xmlns="*">
- <mx:Script>
- <![CDATA[
- import mx.core.UIComponent;
- import mx.controls.Button;
- public var control:UIComponent;
- override protected function childrenCreated():void
- {
- if (control != null)
- {
- myControlBar.addChild(control);
- }
- super.childrenCreated();
- }
- ]]>
- </mx:Script>
- <mx:Label x="111.5" y="70" text="Welcome to myComponent"/>
- <mx:ControlBar id="myControlBar" horizontalAlign="center">
- <!--new Object goes here -->
- </mx:ControlBar>
- </mx:Panel>
爲了能演示效果,我們把子類改成一個application
SubApplication:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns="*">
- <MyComponent>
- <!-- control is the property defined in MyComponent-->
- <control>
- <mx:Button label="OK"/>
- </control>
- </MyComponent>
- </mx:Application>