1

鉴于以下情况:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2009/mxml">
    <mx:Panel title="blah">
       <mx:Button />
    </mx:Panel>
</mx:Application>

您能告诉我默认情况下 mxmlc 在父元素(例如 mx:Panel)中分配子元素(例如 mx:Button)的位置吗?您可以设置“ DefaultProperty ”编译器元数据标记来指定它们被分配的位置,但是当没有指定时 flex 会做什么。

例如,我遍历了所有 flex 类 mx:Panel 继承自的源,但从未提及 DefaultProperty,这让我想知道 DefaultProperty 的默认值是什么。

抱歉有任何无聊,但我已经彻底阅读了文档。

4

2 回答 2

3

在编写基于 AS 的组件时,默认属性允许您指定可用作子标记的属性。例如:

 <MyComp:TextAreaDefaultProp>Hello</MyComp:TextAreaDefaultProp>

您也可以使用:

 <MyComp:TextAreaDefaultProp defaultText="Hello" />

如果不指定会发生什么?您没有获得该属性的值。给定以下组件:

package
{
    // as/myComponents/TextAreaDefaultProp.as
    import mx.controls.TextArea;

    // Define the default property.
    [DefaultProperty("defaultText")]

    public class TextAreaDefaultProp extends TextArea {

        public function TextAreaDefaultProp() 
        {
            super();
        }       

        // Define a setter method to set the text property
        // to the value of the default property.
        public function set defaultText(value:String):void {
            if (value!=null)
            text=value;
        }

        public function get defaultText():String {
            return text;
        }
    }    

}

运行此代码段:

 <?xml version="1.0" encoding="utf-8"?>
 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
            layout="absolute" width="535" height="345"
            xmlns:local="*">
 <mx:VBox>
 <local:TextAreaDefaultProp id="a" defaultText="Hello"/>
 <local:TextAreaDefaultProp id="b" > World </local:TextAreaDefaultProp>
 <local:TextAreaDefaultProp id="c" />
 <mx:TextArea id="z"/>
 <mx:Button  click="{z.text = a.defaultText 
                                + ' ' + b.defaultText
                                + ' ' + (c.defaultText.length);}" />

</mx:VBox>
</mx:Application>
于 2009-03-30T18:35:27.030 回答
1

编译器实际上将容器的子组件视为一种特殊情况。看一下childDescriptors属性以mx.core.Container获得一些解释。当您在 MXML 中创建 Flex 组件实例时,它不会立即实例化。相反,会创建一个“描述符”,用于在未来某个时间实例化组件,具体取决于容器的creationPolicy属性。如果将-keep-generated-actionscript参数(或缩短的版本-keep)添加到编译器参数,您将能够看到编译器从 MXML 生成的 AS3 代码。

于 2009-04-02T16:18:01.623 回答