将属性传递给Flash Builder 4中的自定义组件

时间:2010-09-07 13:06:18

标签: flex actionscript-3 actionscript flex4 mxml

我正在尝试将一些属性传递给我在Flash Builder 4中创建的组件。在下面的示例中,我想传递“label”属性来更新Button的label属性。

非常感谢任何帮助。提前谢谢。

// MyApp.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       xmlns:local="*">
    <fx:Script>
        <![CDATA[
            protected function buttonText():void
            {
                myButton.label = 'Clicked!';
            }
        ]]>
    </fx:Script>
<local:MyComp id="myButton" label="My Button" click="buttonText()"/>
</s:WindowedApplication>

// MyComp.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx"
         width="400" height="300">
    <s:Button/>
</s:Group>

1 个答案:

答案 0 :(得分:2)

<fx:Script>
    <![CDATA[
        private var _label:String;

        public function get label() : String {
          return _label;
        }
        public function set label(value:String) : void {
          _label = value;
          myButton.label = value;
        }

        protected function buttonText():void
        {
            myButton.label = 'Clicked!';
        }
    ]]>
</fx:Script>

这将在控件的label属性和myButton.label的label属性之间创建一个默认绑定。您还可以在label属性的getter上使用[Bindable]元标记。

无论哪种方式,您只需设置组件的label属性,myButton标签的值将反映新值。