增加属性和方法到一个组件
1.在AS里定义public属性
1)在组件里使用public定义的属性,或用getter和setter定义的属性都可以在MXML标签中直接指定值,如:
<MyComp:MyCustomComponent prop1="3"/>
2)下例,可以让用户使用Ctrl+I键来增加TextArea控件的字体尺寸,用Ctrl+M来缩小。package myComponents
{
// as/myComponents/TextAreaFontControl.as
import mx.controls.TextArea;
import flash.events.KeyboardEvent;
import flash.events.Event;
public class TextAreaFontControl extends TextArea
{
// Constructor
public function TextAreaFontControl() {
super();
// Add event listeners.
addEventListener("keyDown", myKeyDown);
addEventListener("creationComplete", myCreationComplete);
}
// Define private var for current font size.
private var currentFontSize:Number;
// Define a public property for the minimum font size.
public var minFontSize:Number = 5;
// Define a public property for the maximum font size.
public var maxFontSize:Number = 15;
// Initialization event handler for getting default font size.
private function myCreationComplete(eventObj:Event):void {
// Get current font size
currentFontSize = getStyle('fontSize');
//你必须在调用getStyle()之前等待组件创建完成,以确保FLEX已经设置了style的值,所以将这一步放在该事件里。然而,你可以调用setstyle()在构造组件时。
}
// keyDown event handler.
private function myKeyDown(eventObj:KeyboardEvent):void {
// Was Ctrl key pressed?
if (eventObj.ctrlKey)
{
switch (eventObj.keyCode) {
// Was Ctrl-I pressed?
case 73 :
if (currentFontSize < maxFontSize) {
currentFontSize = currentFontSize + 1;
setStyle('fontSize', currentFontSize);
}
break;
// Was Ctrl-M pressed?
case 77 :
if (currentFontSize > minFontSize) {
currentFontSize = currentFontSize - 1;
setStyle('fontSize', currentFontSize);
}
break;
default :
break;
}
}
}
}
}
在MXML里调用:
<MyComp:TextAreaFontControl id="myTAFS"
minFontSize="8"
maxFontSize="50"/>
3)推荐使用getter和setter方法来设置属性,而不是直接使用public属性,这样它可以做些其他事,如:
public function set maxFontSize(value:Number):void {
if (value <= 30) {
_maxFontSize = value;
} else _maxFontSize = 30;
}进行值的校验
4)创建默认属性
在AS组件里使用[DefaultProperty]标签可以指定默认属性,默认属性就是在MXML里作为组件的子标签,而不用指出它的名字的。
package myComponents
{
// 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;
}
}
}
在MXML里使用:
<?xml version="1.0"?>
<!-- as/MainTextAreaDefaultProp.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myComponents.*">
<MyComp:TextAreaDefaultProp>Hello</MyComp:TextAreaDefaultProp>
</mx:Application>
但有一个时候不能以这种方式使用这种默认属性,就是组件是MXML的根标签的时候,如:
<?xml version="1.0"?>
<!-- as/myComponents/TextAreaDefaultPropMXML.mxml -->
<MyComp:TextAreaDefaultProp xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myComponents.*">
<MyComp:defaultText>Hello</MyComp:defaultText>
</MyComp:TextAreaDefaultProp>
这个时候,你必须明确地指定子标签。
5)让属性在Flex Bulider里可检查的(好像是可以编码提示),通过在属性(或getter和setter方法)前面增加[Inspectable]标签。如:
[Inspectable] var prop1:Number;