apache-flex - 有人可以向我解释这个常见的绑定(bind)陷阱吗? (使用错误的可绑定(bind)事件名称)

标签 apache-flex binding gumbo

我指的是这个网站link text

Using the wrong event name in the [Bindable] tag can cause your application to not bind your property, and you will not even know why. When you use the [Bindable] tag with a custom name, the example below looks like a good idea:

public static const EVENT_CHANGED_CONST:String = "eventChangedConst"; 
private var _number:Number = 0; 
[Bindable(event=EVENT_CHANGED_CONST)] 
public function get number():Number 
{ 
  return _number; 
} 
public function set number(value:Number) : void 
{ 
  _number = value; 
  dispatchEvent(new Event(EVENT_CHANGED_CONST)); 
}

The code above assigns a static property to the event name, and then uses the same assignment to dispatch the event. However, when the value changes, the binding does not appear to work. The reason is that the event name will be EVENT_CHANGED_CONST and not the value of the variable.

The code should have been written as follows:

public static const EVENT_CHANGED_CONST:String = "eventChangedConst"; 
private var _number:Number = 0; 
[Bindable(event="eventChangedConst")] 
public function get number():Number 
{ 
  return _number; 
} 
public function set number(value:Number) : void 
{ 
  _number = value; 
  dispatchEvent(new Event(EVENT_CHANGED_CONST)); 
}

我同意,错误的例子看起来确实是个好主意,我会这样做,因为我认为这是正确的方式,并且避免了输入错误的可能性。为什么使用常量的名称而不是它的值?这肯定不对吧?

感谢您的见解

最佳答案

因为标准的 Flex 编译器有时并不那么聪明……我能感受到你的痛苦!我已经多次提示过这个确切的问题。

如果我没记错的话,那是因为编译器进行了多次传递。早期的过程之一将元数据更改为 AS 代码。此时在编译器中它还没有解析其余的 AS 代码,因此它无法解析常量或对其他文件中静态变量的引用。

我唯一可以建议的是注册 Adob​​e JIRA,为错误投票,并希望 4.5 中的编译器修复能带来一些缓解。

关于apache-flex - 有人可以向我解释这个常见的绑定(bind)陷阱吗? (使用错误的可绑定(bind)事件名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4050013/

相关文章:

apache-flex - 如何在 ActionScript 3 中从类实例获取 Class 对象?

image - 如何在 Flex/Spark TextArea 或 TextFlow 中的特定位置添加图像

wpf - 在 VisualState Storyboard 中使用绑定(bind)

swift - 如何在 SwiftUI 中分配可选的绑定(bind)参数?

apache-flex - 具有 Flex4 设置的复杂 Maven2

apache-flex - Flex 4 中的自定义预加载器?

java - 执行代理查找时出现重复 session 错误

php - Flex 3 或 Flash Builder 4 开发教会管理系统

layout - 将 TextArea 高度绑定(bind)到其内容