2012-09-07 42 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

相關問題