Flex 中的某些 [Bindable] 属性有效,有些则无效
问题已解决,见下文
问题
我正在 Flex Builder 3 中工作,我有两个 ActionScript 3 类(ABC
和 XYZ< /code>)和 Flex MXML 项目 (
main.mxml
)。 我有一个 XYZ
实例作为 ABC
的属性,并且我希望 XYZ
的属性可见([Bindable]
)在 Flex 项目中的文本控件中。
不幸的是,只有 prop3
和 prop4
在更改时更新。 我已进入调试器以确保 prop1
和 prop2
发生变化,但它们并未在文本控件中更新。
这是代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来你的吸气剂正在返回空值。 根据您的字段类型,他们应该返回单位。
否则你的代码应该可以正常工作。 我已经组装并测试了一个带有设置所有四个值的计时器的工作版本,因此您可以看到所有四个更新:
Main.mxml:
ABC.as
XYZ.as
看看这些,将其插入,您应该会看到它们全部组合在一起。 如果您有任何疑问,请回帖。 干杯。
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:
ABC.as
XYZ.as
Have a look at those, plug things in and you should see it all come together. Post back if you have any questions. Cheers.
当您通过使用 getter 和 setter 定义可绑定源时,绑定似乎不起作用。 解决方案是为您的 getter 声明一个可绑定事件,并在 setter 中调度该事件:
因此,每当您的私有成员发生更改时,就会调度一个事件,通知链接到 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:
So whenever your private member changes, an event is dispatched that notifies any component linked to the getter that the property has changed.