编译器警告我,绑定不起作用,但为什么我运行它确实有效的应用程序!

Cap*_*ble 3 apache-flex adobe actionscript

下面的Flex应用程序生成编译器警告:数据绑定将无法检测到'dp'的分配.这似乎是正确的,因为变量'dp'不是可绑定属性(没有[Bindable]元数据标记).我添加了一个按钮,当点击它时,它会将项目附加到'dp'的后面.虽然编译器警告我不会看到'dp'的更改,但每次单击按钮时列表都会显示新项目!

我不明白为什么我可以在列表中看到新项目.有人可以解释为什么这仍然有效虽然'dp'不可绑定吗?

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600">
<mx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.rpc.events.ResultEvent;

        private var arrayData:Array = [
            {name:"banana", cat:"fruit", cost:0.99},
            {name:"bread", cat:"bakery", cost:1.99},
            {name:"orange", cat:"fruit", cost:0.52},
            {name:"donut", cat:"bakery", cost:0.33},
            {name:"apple", cat:"fruit", cost:1.05}];

        private var dp:ArrayCollection = new ArrayCollection(arrayData);

        private function onButtonClick(event:MouseEvent):void
        {
            var obj:Object = new Object();
            obj.name="test";
            obj.cat="testcat";
            obj.cost=666;
            dp.addItem(obj);
        }
    ]]>
</mx:Script>        
<mx:HorizontalList dataProvider="{dp}" labelField="name" columnWidth="100" width="80%" height="50"/>
<mx:Button label="Click me" click="onButtonClick(event)" /> 
Run Code Online (Sandbox Code Playgroud)

Mar*_*itt 5

编译器的警告是正确的.

编译器警告您,将不会检测到将您指定dp的初始值更改ArrayCollection为另一个的值的分配ArrayCollection.

但是,如果您保留dp单独的值,并且只更改其内容,那么您<HorizontalList />将继续工作.

这可能看起来微不足道,但它是一个重要的区别,并且可以在您的应用程序中进一步导致一些非常混乱的错误.

dp不会检测到变量的分配.然而,对ArrayCollections的更改list将会因为他们发送一个CollectionChangeEvent.

例如:

private var dp:ArrayCollection = new ArrayCollection();

private function test():void
{
    // Here, we don't change the value of dp directly,
    // instead we just modify it's list.
    // The DataGroup will show the strings One,Two
    dp.addItem("One")
    dp.addItem("Two") 

    // Here, we change the actual value of dp, by assigning a 
    // new ArrayCollection to it.
    // This change would not be detected, and the list would continue to show 
    // the contents of the previous value.
    // Additionally, the label will show the string "Length: 2",
    // even though the length is clearly now 3. 
    dp = new ArrayCollection(); 
    dp.addItem("Tahi");
    dp.addItem("Rua");
    dp.addItem("Toru");
}


<s:DataGroup dataProvider="{dp}" />
<s:Label text="Length: {dp.length}" />
Run Code Online (Sandbox Code Playgroud)