2011-03-14 97 views
0

我只是不明白這一點與[綁定]和更新標籤使用[綁定] Flex 4中

因此,這裏有我的三個頁面,請告訴我,我做錯了,因爲當讀心人發送var更新爲button2.mxml var更新,但標籤不重繪它。

application.mxml

<s:WindowedApplication 
xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx" 
xmlns:comps="components.*" 
creationComplete="init();"> 

<fx:Script> 
    <![CDATA[ 
     import mx.controls.Alert; 

     public function init(){ 
     btn1.addEventListener(MouseEvent.MOUSE_OVER, myFunction); 
     } 
     public function myFunction(e:MouseEvent){ 
      var myPage:button2 = new button2(); 
      var ranNum = Math.floor(Math.random() * 40) + 10; 
      myPage.myValue("ABC "+ranNum); 
     } 
    ]]> 
</fx:Script> 
<comps:button1 y="0" id="btn1" width="100"/> 

<comps:button2 y="100" id="btn2" width="100"/> 

button1.mxml

<?xml version="1.0" encoding="utf-8"?> 
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx" width="128" height="72"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <s:Button x="27" y="19" label="Button1" id="btn1" enabled="true"/> 
</s:Group> 

button2.mxml

<?xml version="1.0" encoding="utf-8"?> 
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <fx:Script> 
     <![CDATA[ 
      [Bindable] 
      public var myVal:String = "Button2"; 

      public function myValue(mV:String) 
      { 
       myVal = mV; 
      } 
     ]]> 
    </fx:Script> 
    <s:Button x="10" y="32" label="{myVal}" id="btn2" enabled="true"/> 
</s:Group> 

回答

1

你的功能應該是:

public function myFunction(e:MouseEvent){ 
    var ranNum = Math.floor(Math.random() * 40) + 10; 
    btn2.myValue("ABC " + String(ranNum)); 
} 

在你有的功能中,你正在創建一個新按鈕(不把它作爲孩子添加到任何東西),並在該按鈕上設置標籤,而不是你在應用程序中定義的標籤。

對於button2.mxml中的標籤,您也不一定需要[Bindable]變量,但這取決於您正在完成的操作。您可以選擇在myValue()函數定義中做btn2.label = mV;

+0

Thankyou @Matt&@Wade,Matt,你的解決方案工作我從來沒有想過刪除「= new」這一行,因爲我認爲需要引用接收頁面。現在,我如何訪問flex4函數以獲取我生命中最後4天的時間? Thx再次。 – user654684 2011-03-14 22:29:36

+0

我回答你提出的問題,但韋德的答案也很有幫助,可能更好。就像我說的,這取決於你在做什麼,這樣可以直接訪問公共可綁定變量,使用setter函數或使用自己的自定義函數。 – 2011-03-15 01:59:29

1

要做到這一點,最簡單的方法是刪除你的制定者在設爲myVal和button2.mxml剛剛成立就像你在myFunction的價值任何其他公共變量:

myPage.myVal = "ABC " + ranNum; 

的原因,你的代碼是不是目前工作是你已經隱式地覆蓋了myVal setter,而不是分派數據改變事件,這是綁定工作的原因。將[Bindable]元數據標記添加到變量時,編譯器會自動爲該變量生成一個setter,併爲您分配適當的事件。

希望有所幫助。