2012-09-07 43 views
2

对不起,我是新来使用Adobe AIR和我不知道为什么我的Adob​​e空气的addEventListener不登记

addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn);

addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop);

线未注册。这是给我的一个错误,指出“号召性可能未定义的方法的addEventListener”

我试图加载应用程序,并使用从下面的代码示例:http://www.mikechambers.com/blog/2007/11/07/air-example-native-drag-and-drop/

这是我DragAndDropExampleClass.as:http://pastebin.com/SNZyW5Cx

在此先感谢!

回答

1

在Mike Chamber的示例中,文件DragAndDropExampleClass中的代码旨在用作内联代码,它不是实际的类。

因此,您删除了包/类指令。这应该可以解决您遇到的问题(如评论足迹中所述)。

然而,包含代码内嵌脚本标记<mx:Script source="DragAndDropExampleClass.as" />是一种不好的做法,也许会导致您的困惑。我把你的两个文件合并成一个应用程序。它和迈克钱伯斯的例子完全一样,只是在一个文件中。也许这会有所帮助。我运行这个应用程序,它的工作原理。

注意:我使用Flash Builder 4.6编译它,所以代码有点不同。如果您使用的是Flex 3,请尝试将Mike的两段代码像我一样。

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" 
         creationComplete="onCreationComplete()"> 
    <fx:Script> 
     <![CDATA[ 
      import flash.desktop.ClipboardFormats; 
      import flash.desktop.NativeDragManager; 
      import flash.display.Sprite; 
      import flash.events.NativeDragEvent; 
      import flash.filesystem.File; 
      import flash.filesystem.FileMode; 
      import flash.filesystem.FileStream; 

      //called when app has initialized and is about to display 
      protected function onCreationComplete():void 
      { 
       //register for the drag enter event 
       addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn); 

       //register for the drag drop event 
       addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop); 
      } 

      //called when the user drags an item into the component area 
      protected function onDragIn(e:NativeDragEvent):void 
      { 
       //check and see if files are being drug in 
       if(e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)) 
       { 
        //get the array of files 
        var files:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array; 

        //make sure only one file is dragged in (i.e. this app doesn't 
        //support dragging in multiple files) 
        if(files.length == 1) 
        { 
         //accept the drag action 
         NativeDragManager.acceptDragDrop(this); 
        } 
       } 
      } 

      //called when the user drops an item over the component 
      protected function onDragDrop(e:NativeDragEvent):void 
      { 
       //get the array of files being drug into the app 
       var arr:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array; 

       //grab the files file 
       var f:File = File(arr[0]); 

       //create a FileStream to work with the file 
       var fs:FileStream = new FileStream(); 

       //open the file for reading 
       fs.open(f, FileMode.READ); 

       //read the file as a string 
       var data:String = fs.readUTFBytes(fs.bytesAvailable); 

       //close the file 
       fs.close(); 

       //display the contents of the file 
       outputField.text = data; 
      } 
     ]]> 
    </fx:Script> 


    <mx:TextArea top="10" right="10" bottom="10" left="251" 
       id="outputField" /> 
    <mx:Text text="Drag a Text File into the Application" 
      width="233" height="148" top="11" left="10"/> 
</s:WindowedApplication> 
+0

感谢您的帮助。我得看看现在这意味着什么! – Richard

+0

我刚编辑我的答案。第一个是正确的(你的应用程序会编译),但是如果该类仅仅是一个EventDispatcher,你的应用程序就不会运行:) –

+0

你是否知道如何让我的outputField在我的类中被识别?在主应用程序中有一个id =“outputField”的textArea,但它不在我的DragAndDropExample类中注册。主要应用程序在这里:http://pastebin.com/iYCYuTjh – Richard

相关问题