如何手动调用flex绑定?

时间:2014-08-04 08:10:02

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

我有一个用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,因为值appObj未更改,并且不会调用绑定(Bindable)。

有人可以帮我解决一下如何使用BindingManager或其他工具手动调用绑定。

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

1 个答案:

答案 0 :(得分:0)

最简单和最简单的方法是双向绑定(只需在{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>

P.S。绑定只是调度和接收事件而不是其他任何东西,所以你可以轻松地自定义它。