Flex 中的某些 [Bindable] 属性有效,有些则无效

发布于 2024-07-26 03:11:56 字数 1744 浏览 10 评论 0原文

问题已解决,见下文

问题

我正在 Flex Builder 3 中工作,我有两个 ActionScript 3 类(ABCXYZ< /code>)和 Flex MXML 项目 (main.mxml)。 我有一个 XYZ 实例作为 ABC 的属性,并且我希望 XYZ 的属性可见([Bindable])在 Flex 项目中的文本控件中。

不幸的是,只有 prop3prop4 在更改时更新。 我已进入调试器以确保 prop1prop2 发生变化,但它们并未在文本控件中更新。

这是代码:

ABC.as

[Bindable]
public class ABC extends UIComponent {
    /* Other properties */

    public var xyz:XYZ = new XYZ();

    /* Methods that update xyz */
}

XYZ.as

[Bindable]
public class XYZ extends Object {
     private var _prop1:uint = 0;
     private var _prop2:uint = 0;
     private var _prop3:uint = 0;
     private var _prop4:uint = 1;

     public function get prop1():uint {
         return _prop1;
     }

     public function set prop1(value:uint):void {
         _prop1 = value;
     }

     /* getters and setters for prop2, prop3, and prop4 */
}

main.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:com="com.*" />
    <com:ABC id="abc" />
    <mx:Text text="{abc.xyz.prop1}" />
    <mx:Text text="{abc.xyz.prop2}" />
    <mx:Text text="{abc.xyz.prop3}" />
    <mx:Text text="{abc.xyz.prop4}" />
</mx:Application>

答案

从我发布的小代码片段中看不出来,但从 XYZ 内部我正在更新 _prop3 和 < code>_prop4 使用它们的设置器。 相比之下,我通过它们的私有变量而不是它们的 setter 更新了 _prop1_prop2。 因此,属性 1 和 2 没有调度更新事件。

Problem solved, see below

Question

I'm working in Flex Builder 3 and I have two ActionScript 3 classes (ABC and XYZ) and a Flex MXML project (main.mxml). I have an instance of XYZ as a property of ABC, and I want XYZ's properties to be visible ([Bindable]) in the Flex project in text controls.

Unfortunately, only prop3 and prop4 update when they are changed. I've entered the debugger to make sure that prop1 and prop2 change, but they are not being updated in the text controls.

Here's the code:

ABC.as

[Bindable]
public class ABC extends UIComponent {
    /* Other properties */

    public var xyz:XYZ = new XYZ();

    /* Methods that update xyz */
}

XYZ.as

[Bindable]
public class XYZ extends Object {
     private var _prop1:uint = 0;
     private var _prop2:uint = 0;
     private var _prop3:uint = 0;
     private var _prop4:uint = 1;

     public function get prop1():uint {
         return _prop1;
     }

     public function set prop1(value:uint):void {
         _prop1 = value;
     }

     /* getters and setters for prop2, prop3, and prop4 */
}

main.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:com="com.*" />
    <com:ABC id="abc" />
    <mx:Text text="{abc.xyz.prop1}" />
    <mx:Text text="{abc.xyz.prop2}" />
    <mx:Text text="{abc.xyz.prop3}" />
    <mx:Text text="{abc.xyz.prop4}" />
</mx:Application>

Answer

It's not apparent from the small code snippets that I posted, but from within XYZ I was updating _prop3 and _prop4 using their setters. In constrast, I updated _prop1 and _prop2 through their private variables, not their setters. Thus, properties 1 and 2 were not dispatching update events.

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

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

发布评论

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

评论(2

Smile简单爱 2024-08-02 03:11:56

看起来你的吸气剂正在返回空值。 根据您的字段类型,他们应该返回单位。

否则你的代码应该可以正常工作。 我已经组装并测试了一个带有设置所有四个值的计时器的工作版本,因此您可以看到所有四个更新:

Main.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*" creationComplete="onCreationComplete()">

    <mx:Script>
        <![CDATA[

            private function onCreationComplete():void
            {
                var t:Timer = new Timer(1000);
                t.addEventListener(TimerEvent.TIMER, t_tick);
                t.start();
            }

            private function t_tick(event:TimerEvent):void
            {
                var i:uint = Timer(event.currentTarget).currentCount;

                abc.xyz.prop1 = i;
                abc.xyz.prop2 = i;
                abc.xyz.prop3 = i;
                abc.xyz.prop4 = i;
            }

        ]]>
    </mx:Script>

    <local:ABC id="abc" />

    <mx:VBox>
        <mx:Text text="{abc.xyz.prop1}" />
        <mx:Text text="{abc.xyz.prop2}" />
        <mx:Text text="{abc.xyz.prop3}" />
        <mx:Text text="{abc.xyz.prop4}" />
    </mx:VBox>

</mx:Application>

ABC.as

package
{
    import mx.core.UIComponent;

    [Bindable]
    public class ABC extends UIComponent
    {
        public var xyz:XYZ = new XYZ();

        public function ABC()
        {
            super();
        }
    }
}

XYZ.as

package
{
    [Bindable]
    public class XYZ extends Object
    {
        private var _prop1:uint = 0;
        private var _prop2:uint = 0;
        private var _prop3:uint = 0;
        private var _prop4:uint = 1;

        public function XYZ()
        {
            super();
        }

        public function get prop1():uint {
            return _prop1;
        }

        public function set prop1(value:uint):void {
            _prop1 = value;
        }

        public function get prop2():uint {
            return _prop2;
        }

        public function set prop2(value:uint):void {
            _prop2 = value;
        }

        public function get prop3():uint {
            return _prop3;
        }

        public function set prop3(value:uint):void {
            _prop3 = value;
        }

        public function get prop4():uint {
            return _prop4;
        }

        public function set prop4(value:uint):void {
            _prop4 = value;
        }
    }
}

看看这些,将其插入,您应该会看到它们全部组合在一起。 如果您有任何疑问,请回帖。 干杯。

It looks like your getters are returning voids. They should be returning uints, according to your field types.

Otherwise your code should be working fine. I've assembled and tested a working version with a Timer that sets all four values, so you can see all four updating:

Main.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*" creationComplete="onCreationComplete()">

    <mx:Script>
        <![CDATA[

            private function onCreationComplete():void
            {
                var t:Timer = new Timer(1000);
                t.addEventListener(TimerEvent.TIMER, t_tick);
                t.start();
            }

            private function t_tick(event:TimerEvent):void
            {
                var i:uint = Timer(event.currentTarget).currentCount;

                abc.xyz.prop1 = i;
                abc.xyz.prop2 = i;
                abc.xyz.prop3 = i;
                abc.xyz.prop4 = i;
            }

        ]]>
    </mx:Script>

    <local:ABC id="abc" />

    <mx:VBox>
        <mx:Text text="{abc.xyz.prop1}" />
        <mx:Text text="{abc.xyz.prop2}" />
        <mx:Text text="{abc.xyz.prop3}" />
        <mx:Text text="{abc.xyz.prop4}" />
    </mx:VBox>

</mx:Application>

ABC.as

package
{
    import mx.core.UIComponent;

    [Bindable]
    public class ABC extends UIComponent
    {
        public var xyz:XYZ = new XYZ();

        public function ABC()
        {
            super();
        }
    }
}

XYZ.as

package
{
    [Bindable]
    public class XYZ extends Object
    {
        private var _prop1:uint = 0;
        private var _prop2:uint = 0;
        private var _prop3:uint = 0;
        private var _prop4:uint = 1;

        public function XYZ()
        {
            super();
        }

        public function get prop1():uint {
            return _prop1;
        }

        public function set prop1(value:uint):void {
            _prop1 = value;
        }

        public function get prop2():uint {
            return _prop2;
        }

        public function set prop2(value:uint):void {
            _prop2 = value;
        }

        public function get prop3():uint {
            return _prop3;
        }

        public function set prop3(value:uint):void {
            _prop3 = value;
        }

        public function get prop4():uint {
            return _prop4;
        }

        public function set prop4(value:uint):void {
            _prop4 = value;
        }
    }
}

Have a look at those, plug things in and you should see it all come together. Post back if you have any questions. Cheers.

一场春暖 2024-08-02 03:11:56

当您通过使用 getter 和 setter 定义可绑定源时,绑定似乎不起作用。 解决方案是为您的 getter 声明一个可绑定事件,并在 setter 中调度该事件:

[Bindable]
public class XYZ extends Object {
     private var _prop1:uint = 0;
     private var _prop2:uint = 0;
     private var _prop3:uint = 0;
     private var _prop4:uint = 1;

     [Bindable(event="prop1Changed")]
     public function get prop1():uint {
         return _prop1;
     }

     public function set prop1(value:uint):void {
         _prop1 = value;
         dispatchEvent (new Event ("prop1Changed"));
     }

     /* getters and setters for prop2, prop3, and prop4 */
}

因此,每当您的私有成员发生更改时,就会调度一个事件,通知链接到 getter 的任何组件该属性已更改。

When you define your bindable sources through the use of getters and setters, the bindings don't seem to work. The solution is to declare a bindable event for your getter and dispatch the event in your setter:

[Bindable]
public class XYZ extends Object {
     private var _prop1:uint = 0;
     private var _prop2:uint = 0;
     private var _prop3:uint = 0;
     private var _prop4:uint = 1;

     [Bindable(event="prop1Changed")]
     public function get prop1():uint {
         return _prop1;
     }

     public function set prop1(value:uint):void {
         _prop1 = value;
         dispatchEvent (new Event ("prop1Changed"));
     }

     /* getters and setters for prop2, prop3, and prop4 */
}

So whenever your private member changes, an event is dispatched that notifies any component linked to the getter that the property has changed.

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