Apache flex Flex:绑定到MXML样式;“绑定字符串”;动作脚本?

Apache flex Flex:绑定到MXML样式;“绑定字符串”;动作脚本?,apache-flex,data-binding,actionscript,mxml,Apache Flex,Data Binding,Actionscript,Mxml,是否可以在ActionScript中指定MXML样式的“绑定字符串” 例如,我希望能够做到以下几点: MXMLBinding(this, "first_item", this, "{myArrayCollection.getItemAt(0)"); MXMLBinding(this, ["nameLbl", "text"], this, "Name: {somePerson.first} {somePerson.last}"); 编辑:谢谢你的回

是否可以在ActionScript中指定MXML样式的“绑定字符串”

例如,我希望能够做到以下几点:

MXMLBinding(this, "first_item",
            this, "{myArrayCollection.getItemAt(0)");
MXMLBinding(this, ["nameLbl", "text"],
            this, "Name: {somePerson.first} {somePerson.last}");

编辑:谢谢你的回复和建议…基本上,你似乎不能这样做。我已经找到了原因。

我可以使用
BindingUtils
ChainWatcher
,但我最终得到的代码看起来像这样:

…
    BindingUtils.bindSetter(updateName, this, ["somePerson", "first"]);
    BindingUtils.bindSetter(updateName, this, ["somePerson", "last"]);
…
protected function updateName(...ignored):void {
    this.nameLbl.text = "Name: " + somePerson.first + " " + somePerson.last;
}
这只是有点难看…第一个例子,绑定到
arrayCollection.getItemAt(0)
,更糟糕。

使用ChangeWatcher(例如,通过BindingUtils.bindProperty或.bindSetter)是一种方法,是的。我承认这是一个奇怪的符号,但一旦你习惯了它,它就有意义,工作完美,也非常灵活

当然,如果符号错误,您可以自己包装这些函数——这两种方法都是静态的,因此以更适合您的应用程序的方式包装这些函数应该是一个相当简单的练习。

第一个参数(函数)BindingUtils.bindSetter的方法是否接受匿名方法

BindingUtils.bindSetter(function()
  {
    this.nameLbl.text = "Name: " + somePerson.first + " " + somePerson.last;
  }, this, ["somePerson", "last"]);

我讨厌匿名方法,显然它更难看——所以我不建议这样做,即使它有效,但我只是想知道它是否有效。

从来没有人想听的答案,只是在ActionScript中使用getter/setter来管理这些东西。使用适当的MVC,手动设置显示字段非常简单

public function set myArrayCollection(value:Array):void {
  myAC = new ArrayCollection(value);
  first_item = mcAC.getItemAt(0);  // or value[0];
}

等等……

好的,我已经做了一些挖掘,下面是发生的事情

与此相反,MXML中的绑定是在编译时由Java代码(
modules/compiler/src/Java/flex2/compiler/as3/binding/DataBindingFirstPassEvaluator.Java
)设置的

例如,绑定:
first_item=“{myArrayCollection.getItemAt(0)
}”被扩展为以下内容:

    // writeWatcher id=0 shouldWriteSelf=true class=flex2.compiler.as3.binding.PropertyWatcher shouldWriteChildren=true
    watchers[0] = new mx.binding.PropertyWatcher("foo",
                                                 { propertyChange: true }, // writeWatcherListeners id=0 size=1
                                                 [ bindings[0] ],
                                                 propertyGetter);

    // writeWatcher id=1 shouldWriteSelf=true class=flex2.compiler.as3.binding.FunctionReturnWatcher shouldWriteChildren=true
    watchers[1] = new mx.binding.FunctionReturnWatcher("getItemAt",
                                                       target,
                                                       function():Array { return [ 0 ]; },
                                                       { collectionChange: true }, 
                                                       [bindings[0]],
                                                       null);

    // writeWatcherBottom id=0 shouldWriteSelf=true class=flex2.compiler.as3.binding.PropertyWatcher
    watchers[0].updateParent(target);

    // writeWatcherBottom id=1 shouldWriteSelf=true class=flex2.compiler.as3.binding.FunctionReturnWatcher
    // writeEvaluationWatcherPart 1 0 parentWatcher
    watchers[1].parentWatcher = watchers[0];
    watchers[0].addChild(watchers[1]);
这意味着不可能在运行时设置花括号MXML样式的绑定,因为在ActionScript中不存在执行该绑定的代码。

(无耻的插件)

可以这样做:

Bind.fromProperty(this, "myArrayCollection", itemAt(0))
    .toProperty(this, "first_item");

Bind.fromAll(
    Bind.fromProperty(this, "somePerson.first"),
    Bind.fromProperty(this, "somePerson.last")
    )
    .format("Name: {0} {1}")
    .toProperty(this, "nameLbl.text");

请注意,BindageTools将源对象放在第一位,将目标对象放在最后一位(而BindingUtils将目标对象放在第一位,将源对象放在最后一位)。

大括号{}绑定特定于MXML,在纯AS3中不可用。BindingUtils是一种方法。它的符号并不奇怪……它需要更多的代码,也就是更详细的代码,才能完成完全相同的事情。是的,我只是不认为还有其他方法。但困难的是,它不会绑定到
myAC[0]
…来实现这一点,我必须在myAC中添加一个
ChangeListener
,并且…(好吧,你知道这个故事的其余部分)。我想我的观点是程序员已经很长一段时间没有绑定了。通常一个好的架构就足够了。您可能需要一个通知系统,以便对AC内部的更改做出反应。