在编写基于 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>