在flex 4中使用[Bindable]

我只是不知道[Bindable]和更新标签 所以这是我的三个页面,请告诉我我做错了什么,因为当listner将更新后的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>
    
已邀请:
你的功能应该是:
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;
。     
最简单的方法是在button2.mxml中删除myVal的setter,并像在任何其他公共变量中一样设置myFunction中的值:
myPage.myVal = "ABC " + ranNum;
您的代码当前不起作用的原因是您隐式重写了myVal setter并且没有调度数据更改事件,这是使绑定工作的原因。将[Bindable]元数据标记添加到变量时,编译器会自动为该变量生成一个setter,并为您调整正确的事件。 希望有所帮助。     

要回复问题请先登录注册