2011-03-14 102 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,并为您分配适当的事件。

希望有所帮助。