Allow custom textual data representation in MXML
I have an ActionScript class called Dimension that allows the client to specify a dimension using a value and unit such as "CM" or "Inches". I want to use an instance of this class as a property in MXML, so the user can write
<DimensionView value="2cm"/>
How do I make "2cm" the accepted string value for the dimension? I am guessing that I need to write a parser method in my Dimension class, but I cannot decide which interface I need to implement to provide this functionality.
Can anyone please help?
source to share
One option is to just type the property value
as String
, write a getter and setter for it, and parse there:
/**
* docs here
*/
[Bindable(event="valueChanged")]
public function get value():String
{
return _valueInt.toString();
}
/**
* @private
*/
public function set value(aVal:String):void
{
// parse the aVal String to an int (or whatever) here
_valueInt = parsed_aVal;
dispatchEvent(new Event("valueChanged"));
}
In a related note, the infrastructure components implement a feature that allows percentage signs to be used in some sizing parameters when assigned in MXML using an undocumented metadata field called PercentProxy
. The example below uses the width
getter and setter attribute from mx.core.UIComponent
:
[Bindable("widthChanged")]
[Inspectable(category="General")]
[PercentProxy("percentWidth")]
override public function get width():Number
{
// --snip snip--
}
override public function set width(value:Number):void
{
// --snip snip--
}
source to share