开发者

Why this ActionScript program isn't working?

It shows me an error of :

1120: Access of undefined property myArray. DataGrid.mxml /DataGrid/src line 10

Source Code :

 <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
    <mx:Script>
        <![CDATA[

            import mx.collections.ArrayCollection;

            [Bindable]
            public var myArray:Array = new Array();
            myArray[0] = "Tom"; // string

            [Bindable]
            public var arrColl:ArrayCollection = new ArrayCollection(myArray);

        ]]>
    </mx:Script>

  开发者_运维知识库  <mx:AdvancedDataGrid id="ad"
                         columns="{myArray}"
                         dataProvider="{arrColl}"/>
</mx:Application>

What is the problem ?


You shouldn’t write arbitrary code directly in a script block like that unless you know exactly what you’re doing. Rather, you should do something like this:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                initialize="initialize()">
  <mx:Script>
    <![CDATA[
      import mx.collections.ArrayCollection;

      [Bindable]
      public var myArray:Array = new Array();

      [Bindable]
      public var arrColl:ArrayCollection;

      private function initialize() : void
      {
        myArray[0] = "Tom";
        arrColl = new ArrayCollection(myArray)
      }
    ]]>
  </mx:Script>
  <mx:AdvancedDataGrid columns="{myArray}" dataProvider="{arrColl}"/>
</mx:Application>

Another problem with this code is that myArray[0] = "Tom" will not cause the data grid to be updated. For that, you would have to assign to the variable myArray itself (e.g. myArray = ["Tom"]).


You can't start assigning values to the array in the class definition. You need move your myArray[0] = "Tom"; line inside of a method. If you want it to be happen at initialization, then specify an event handler in the Application tag creationComplete="yourEventHandler", and put the line in yourEventHandler(). Hope that helps, let me know if you need more code :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜