2012-02-11 45 views
0

我想从mxml文件中使用ActionScript DataGrid组件。 但它显示我一些错误。包不能嵌套 - as3

以下是我的主要应用程序文件。

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:local="*" xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"> 



    <mx:Metadata> 
     [Event(name="myEvent", type="flash.events.Event")] 
    </mx:Metadata> 


    <mx:Button label="Button"/> 
    <mx:Array id="arr"> 
      <mx:Object From="Phill" Subject="GMC-1 Release" Date="12/08/06" CC="Jim" Profit="69" /> 
      <mx:Object From="Harry" Subject="GMC-1 Release" Date="12/08/06 11111111" CC="Ram" Profit="10" /> 
      <mx:Object From="Barb" Subject="GMC-1 Release" Date="12/08/06" CC="Anant" Profit="20" /> 
      <mx:Object From="Amit" Subject="GMC-1 Release" Date="12/07/06" CC="Jim" Profit="28" /> 
      <mx:Object From="Sam" Subject="GMC-1 Release" Date="12/08/06" CC="Jim" Profit="17" /> 
      <mx:Object From="Phill" Subject="GMC-2 Release" Date="12/11/06" CC="Jim" Profit="10" /> 
      <mx:Object From="John" Subject="Grid scrolling" Date="12/10/06" CC="Craig" Profit="20" /> 
      <mx:Object From="Bob" Subject="ItemRenderers" Date="12/10/06" CC="Moxie" Profit="11" />    
</mx:Array> 

<local:AutoResizableADG id="adg" dataProvider="{arr}" width="400" height="400" > 
    <local:columns> 
     <mx:AdvancedDataGridColumn headerText="From" dataField="From" width="50" /> 
     <mx:AdvancedDataGridColumn headerText="Subject" dataField="Subject" width="50" /> 
     <mx:AdvancedDataGridColumn headerText="Date" dataField="Date" width="70" /> 
     <mx:AdvancedDataGridColumn headerText="CC" dataField="CC" /> 
    </local:columns> 

</local:AutoResizableADG> 

</mx:Application> 

AutoResizableADG.as文件是在相同的封装..即SRC /(缺省封装)

// ActionScript file 

package  // Line where it is showing error. 
{ 

    import flash.display.DisplayObject; 
    import flash.events.MouseEvent; 
    import flash.text.TextLineMetrics; 

    import mx.controls.AdvancedDataGrid; 
    import mx.controls.Alert; 
    import mx.controls.listClasses.IDropInListItemRenderer; 
    import mx.core.IUIComponent; 
    import mx.core.UIComponent; 

    public class AutoResizableADG extends AdvancedDataGrid 
    { 
     var sepArray:Array = new Array(); 
     public function AutoResizableADG() 
     { 
      // call super 
      super(); 
     } 

     /** 
     * Returns the header separators between column headers, 
     * and populates the <code>separators</code> Array with the separators returned. 
     * 
     * @param i The number of separators to return. 
     * 
     * @param seperators Array to be populated with the header objects. 
     * 
     * @param headerLines The parent component of the header separators. 
     * Flex calls the <code>headerLines.getChild()</code> method internally to return the separators. 
     */ 
     override protected function getSeparator(i:int, seperators:Array, headerLines:UIComponent):UIComponent 
     { 

      var sep:UIComponent = super.getSeparator(i, seperators, headerLines); 
      sep.doubleClickEnabled = true; 
      // Add listener for Double Click 
      DisplayObject(sep).addEventListener(myEvent.myEvent, hello); 
      //   Alert.show(""+sep); 
      sepArray.push(sep); 
      return sep; 
     } 

     public function getListItems():Array{ 
      return listItems; 
     } 

     /** 
     * @private 
     * Indicates where the right side of a resized column appears. 
     */ 
     public function hello(event:UIComponent):void 
     { 
      // check if the ADG is enabled and the columns are resizable 
      if (!enabled || !resizableColumns) 
       return; 

      var target:DisplayObject = DisplayObject(event); 
      var index:int = target.parent.getChildIndex(target); 
      // get the columns array 
      var optimumColumns:Array = getOptimumColumns(); 

      // check for resizable column 
      if (!optimumColumns[index].resizable) 
       return; 

      // calculate the maxWidth - we can optimize this calculation 
      if(listItems) 
      { 
       var len:int = listItems.length; 
       var maxWidth:int = 0; 
       for(var i:int=0;i<len;i++) 
       { 
        if(listItems[i][index] is IDropInListItemRenderer) 
        { 
         var lineMetrics:TextLineMetrics = measureText(IDropInListItemRenderer(listItems[i][index]).listData.label); 
         if(lineMetrics.width > maxWidth) 
          maxWidth = lineMetrics.width ; 
        } 
       } 
      } 

      // set the column's width 
      optimumColumns[index].width = maxWidth + getStyle("paddingLeft") + getStyle("paddingRight") + 8; 
     } 
    } 
} 

它表示我在ActionScript文件中的错误....

"Packages cannot be nested" 

这是怎么发生的?问题是什么 ?

+0

您的代码似乎正确 - 我已将其复制并粘贴到我的编辑器,并且没有显示“程序包无法嵌套”错误。它可能与您的文件夹结构有关吗(无名包总是最常用的源文件夹)?那个超级班呢?你在使用哪个IDE?如果您在Flash Builder或FDT中,请尝试“项目 - >清理”。 – weltraumpirat 2012-02-12 09:34:41

+0

我只使用你的代码设置了一个新项目。我得到了AutoResizableADG类的编译错误,说'myEvent'是一个未定义的属性,这是你的问题吗?我唯一的代码是你的类和上面的应用程序代码,都在默认包中。 – Jeremy 2012-02-29 22:17:58

回答

0

我不确定它为什么不编译,但是您是否尝试将AutoResizableADG.as放入实际包中以查看它是否修复了问题?

E.g.

// ActionScript file 

package myPackage 
{ 
import flash.display.DisplayObject; 
import flash.events.MouseEvent; 
import flash.text.TextLineMetrics; 

import mx.controls.AdvancedDataGrid; 
import mx.controls.Alert; 
import mx.controls.listClasses.IDropInListItemRenderer; 
import mx.core.IUIComponent; 
import mx.core.UIComponent; 

public class AutoResizableADG extends AdvancedDataGrid 
{ 
    .... 
0

这是行为,我称奇怪,但记录。我发现了两个修正:

  1. weltraumpirat的正确。尝试清理你的代码。
  2. 如果你在flashdevelop中,你可以删除myPackge引用, 再次测试项目,当它说出下一个错误时,撤销 更改,它会接受它。

这两种方法在相同的原理下操作。
祝你好运!