为什么这个 ActionScript 程序不工作?

发布于 2024-12-06 21:42:26 字数 825 浏览 1 评论 0原文

它向我显示错误:

1120:访问未定义的属性 myArray。 DataGrid.mxml /DataGrid/src 第 10 行

源代码:

 <?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>

有什么问题?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋叶绚丽 2024-12-13 21:42:26

除非您确切知道自己在做什么,否则不应直接在脚本块中编写任意代码。相反,您应该这样做:

<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>

此代码的另一个问题是 myArray[0] = "Tom" 不会导致数据网格更新。为此,您必须分配给变量 myArray 本身(例如 myArray = ["Tom"])。

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"]).

随遇而安 2024-12-13 21:42:26

您无法开始为类定义中的数组赋值。您需要将 myArray[0] = "Tom"; 行移到方法内部。如果您希望它在初始化时发生,请在应用程序标记 creationComplete="yourEventHandler" 中指定一个事件处理程序,并将该行放入 yourEventHandler() 中。希望有帮助,如果您需要更多代码,请告诉我:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文