flex 3扩展自定义组件

在flex 开发中我想要定义一个组件myComponent,并希望在继承它的组件subComponent里可以添加新的control。
myComponent:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300">
  3.     <mx:Label x="111.5" y="70" text="Welcome to myComponent"/>
  4.     
  5. </mx:Panel>
subComponent:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <MyComponent xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml" >
  3.     <mx:Button />
  4. </MyComponent>
但是编译器会报错:Multiple sets of visual children have been specified for the MyComponent tag.
也即是flex 3不支持子类直接添加新的control 。 必须通过 重写childrenCreated方法来实现。新的代码如下:
MyComponent:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300" xmlns="*">
  3.     <mx:Script>
  4.         <![CDATA[
  5.             import mx.core.UIComponent;
  6.             import mx.controls.Button;
  7.    
  8.       
  9.         public var control:UIComponent;
  10.   
  11.         override protected function childrenCreated():void
  12.         {
  13.             if (control != null)
  14.             {
  15.                 myControlBar.addChild(control);
  16.             }     
  17.            
  18.             super.childrenCreated();
  19.         }
  20.         ]]>
  21.     </mx:Script>
  22.     <mx:Label x="111.5" y="70" text="Welcome to myComponent"/>
  23.     <mx:ControlBar id="myControlBar" horizontalAlign="center">
  24.     <!--new Object goes here -->   
  25.     </mx:ControlBar>
  26. </mx:Panel>

为了能演示效果,我们把子类改成一个application
SubApplication:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns="*">
  3.     <MyComponent>
  4.     <!-- control is the property defined in MyComponent-->
  5.       <control>
  6.         <mx:Button label="OK"/>
  7.       </control>
  8.     </MyComponent>
  9. </mx:Application>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值