在flex 4中使用[Bindable]

发布于 2024-10-22 03:47:13 字数 2193 浏览 1 评论 0原文

我只是不明白 [Bindable] 和更新标签

所以这是我的三个页面,请告诉我我做错了什么,因为当列表器将更新的变量发送到button2.mxml时,变量会更新,但标签不会更新重画它。

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"/>

按钮1.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>

按钮2.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>

I just don't get it with [Bindable] and updating a label

So here are my three pages, please tell me what I am doing wrong because when the listner sends the updated var to button2.mxml the var updates but the label does not redraw it.

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救赎№ 2024-10-29 03:47:13

您的功能应该是:

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;

Your function should be:

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

In the function that you have, you are creating a new button (without adding it as a child to anything) and setting the label on that button rather than the one you have already defined in your application.

You also do not necessarily need a [Bindable] variable for the label in button2.mxml but this depends on what you are accomplishing. You could just do btn2.label = mV; in the myValue() function definition alternatively.

乄_柒ぐ汐 2024-10-29 03:47:13

最简单的方法是删除 Button2.mxml 中 myVal 的设置器,然后像设置任何其他公共变量一样在 myFunction 中设置值:

myPage.myVal = "ABC " + ranNum;

您的代码当前无法工作的原因是您已经隐式覆盖了 myVal 设置器并且不调度数据更改事件,这就是绑定工作的原因。当您将 [Bindable] 元数据标记添加到变量时,编译器会自动为该变量生成一个 setter,并为您分配适当的事件。

希望有帮助。

The simplest way to do this is to remove your setter for myVal in button2.mxml and just set the value in myFunction like you would any other public variable:

myPage.myVal = "ABC " + ranNum;

The reason your code is not currently working is that you have implicitly overridden the myVal setter and are not dispatching a data change event, which is what makes binding work. When you add the [Bindable] metadata tag to a variable, the compiler automatically generates a setter for that variable with the proper event dispatching for you.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文