沒有任何代碼可以通過(請包括這兩個組件的樣本,如果可以的話),有很多方法可以解決這個問題。
您可以添加綁定到所討論變量的ChangeWatcher
,並在屬性更改時調用方法。此方法可以根據適用於屬性的任何條件來更改圖像。
它看起來東西這樣的:
Component 1:
[Bindable]
public var yourVariable:Number;
Component 2:
private var propertyWatcher:ChangeWatcher;
//In some initialization method -- this is JUST an example method for how to create the property watcher
public function init(): void {
...
propertyWatcher = ChangeWatcher.watch(component1, "yourVariable", onVariableUpdate);
}
//Define this as a new method to handle when the property changes
private function onVariableUpdate(event:PropertyChangeEvent):void {
if(event.newValue == 50) {
yourImage.source = newSource;
}
else if(event.newValue == 100) {
yourImage.source = otherSource;
}
}
顯然,這是非常截斷和速記,但希望它會幫助你開始。
編輯:ChangeWatchers確實存在Flex 3中,但它聽起來像是你應該在不同的方向走。由於您發佈的代碼片段有點小,因此我將對如何執行此操作做一些假設:)
正如alxx在他的評論中提到的,您可以將組件中的屬性accountState
更改爲實際的財產,給一個setter/getter。這將允許您在accountState更新時進行更廣泛的處理。
它看起來應該東西這樣的:
MyComp:
//Inside your script tag:
private var _accountState:Number;
[Bindable]
public function get accountState():Number {
return _accountState;
}
public function set accountState(state:Number):void {
_accountState = state;
switch(state) {
case 50:
yourIcon.source = "blahblahblah";
break;
case 100:
yourIcon.source = "blahblahblah2";
break;
//And so on
}
}
這不會更改您發佈的代碼:它應該仍然工作作爲你寫它。我沒有測試過,所以有可能我失去了一些東西,但希望這會有所幫助:)
您可以將變量轉換爲getter/setter並更改setter中的圖片。 – alxx 2011-02-18 19:32:21