【问题标题】:Passing properties to a custom component in Flash Builder 4在 Flash Builder 4 中将属性传递给自定义组件
【发布时间】:2010-09-07 13:06:17
【问题描述】:

我正在尝试将一些属性传递给我在 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>

【问题讨论】:

    标签: apache-flex actionscript-3 actionscript flex4 mxml


    【解决方案1】:
    <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>
    

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

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

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 2021-11-11
      • 2019-04-08
      • 2019-10-09
      • 2018-05-08
      相关资源
      最近更新 更多