【问题标题】:How to invoke flex binding manually?如何手动调用 flex 绑定?
【发布时间】:2014-08-04 08:10:01
【问题描述】:

我有一个用 flex 动作脚本编写的代码,我正在尝试使用 selectedValue 将变量绑定到 RadioButtonGroup

<?xml version="1.0" encoding="utf-8"?>
<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">
    <fx:Script>

        <![CDATA[
            [Bindable]
            public static var appObj:String = "first";

            public function setToThird():void {
                appObj = "third";
            }
            public function setToFirst():void {
                appObj = "first";
            }
        ]]>

    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <s:RadioButtonGroup id="travelGroup" selectedValue="{appObj}"/>
    </fx:Declarations>
    <s:RadioButton x="167" y="91" label="first" group="{travelGroup}"/>
    <s:RadioButton x="167" y="119" label="second" group="{travelGroup}"/>
    <s:RadioButton x="167" y="151" label="third" group="{travelGroup}"/>
    <s:Label x="195" y="177" text="appObj value is {appObj}" />
    <s:Button x="149" y="277" label="setToThird" click="setToThird()"/>
    <s:Button x="270" y="277" label="setToFirst" click="setToFirst()"/>
</s:WindowedApplication>

当我点击setToThird button 时,appObj value 将设置为字符串third,而选中的单选按钮将是third

现在,当我单击 second 单选按钮时,appObj 的值仍然是字符串 third,单击按钮 setToThird 不会将单选按钮选择更改为 third 作为值 @987654331 @ 没有改变,绑定 (Bindable) 不会被调用。

谁能帮助我了解如何使用 BindingManager 或其他工具手动调用绑定。

我不想实现双向数据绑定。

【问题讨论】:

    标签: actionscript-3 flash apache-flex air flash-builder


    【解决方案1】:

    最简单最简单的方法是双向绑定(只需在 {appObj} 之前添加 @ 符号)。但如果你想避免这种情况,你可以采取一些逻辑:

    <?xml version="1.0"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:local="*">
    <fx:Script>
    
        <![CDATA[
        [Bindable(event="appObjChanged")]
        public var appObj:String = "first";
    
        public function setToThird():void {
            appObj = "third";
            dispatchEvent(new Event("appObjChanged"));
        }
        public function setToFirst():void {
            appObj = "first";
            dispatchEvent(new Event("appObjChanged"));
        }
        ]]>
    
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <s:RadioButtonGroup id="travelGroup" selectedValue="{appObj}"/>
    </fx:Declarations>
    <s:RadioButton x="167" y="91" label="first" group="{travelGroup}"/>
    <s:RadioButton x="167" y="119" label="second" group="{travelGroup}"/>
    <s:RadioButton x="167" y="151" label="third" group="{travelGroup}"/>
    <s:Label x="195" y="177" text="appObj value is {appObj}" />
    <s:Button x="149" y="277" label="setToThird" click="setToThird()"/>
    <s:Button x="270" y="277" label="setToFirst" click="setToFirst()"/>
    </s:Application>
    

    附:绑定只是调度和接收事件,没有别的,所以你可以很容易地自定义它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-11
      • 2018-07-08
      • 2011-08-08
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多