2008-12-01 18 views
0

我有一個名爲Dimension的actionscript類,它允許客戶端使用值和單位(如「CM」或「Inches」)指定維度。我想用這個類的一個實例爲MXML中的屬性,因此用戶可以編寫允許MXML中數據的自定義文本表示

<DimensionView value="2cm"/> 

怎樣使「2釐米」爲維度接受的字符串值?我假設我需要在我的Dimension類上編寫一個解析器方法,但是我無法確定應該實現哪個接口來提供此功能。

任何人都可以幫忙嗎?

回答

1

一種選擇是直接輸入value屬性作爲String,寫一個getter和爲它制定者和執行解析有:

/** 
* 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")); 
} 

相關提示,框架組件實現的功能通過使用名爲PercentProxy的未公開元數據字段,允許在MXML中分配時在某些大小特性中使用百分比符號。下面的例子是width屬性獲取器和設置器從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-- 
}