2009-07-22 26 views
1

我剛碰到一個奇怪的綁定問題。在下面的迷你應用程序中,Flex標籤組件在'someText'更改時更新,但在第一次初始調用後不會調用boundSetter。基本綁定問題

簡而言之:爲什麼不調用boundSetterForSomeText()函數,而標籤更新?

請問任何人都可以解釋一下這個基本問題嗎?太感謝了!

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="1024" minHeight="768" 
    initialize="onInitialize()" 
> 
    <mx:Panel> 
     <mx:Label text="{this.someText}" /> 
     <mx:Button label="Set random text" click="generateRandom()" /> 
    </mx:Panel> 

    <mx:Script> 
     <![CDATA[ 
      import mx.binding.utils.ChangeWatcher; 
      import mx.binding.utils.BindingUtils; 


      [Bindable(event="xxx")] 
      public var someText : String; 


      public function onInitialize() : void 
      { 
       var cw:ChangeWatcher = BindingUtils.bindSetter(boundSetterForSomeText, this, ['someText']); 
      } 

      public function generateRandom() : void 
      { 
       this.someText = String(Math.round(Math.random() * 10000)); 
       this.dispatchEvent(new Event("xxx")); 
      } 


      public function boundSetterForSomeText(obj:Object) : void 
      { 
       trace(obj); 
      } 
     ]]> 
    </mx:Script> 
</mx:Application> 

回答

0

您可以使用此代碼來創建一個get/set方法對或 「屬性」:

private var _someText:String; 

[Bindable(event="xxx")] 
public function get someText():String 
{ 
    return _someText; 
} 

public function set someText(value:String):void 
{ 
    if (_someText != value) 
    { 
     _someText = value; 
     this.dispatchEvent(new Event("xxx")); 
    } 
} 
+0

沒錯,你甚至不需要setter,也就是說,你可以用你喜歡的任何函數來改變值/派發事件。但獲得吸氣劑是至關重要的。 – Tom 2009-07-24 06:32:37

0

它在事件是默認情況下工作。 (默認事件是propertyChange)

[Bindable] 
public var someText : String; 

我做了一些調試,我不知道爲什麼它不適用於自定義事件。我認爲它應該。

+0

呀,奇怪..很令人擔心的了。我將把它作爲評論發佈在Adobe的文檔中。 – Tom 2009-07-23 15:16:33