在 Mike Chamber 的示例中,文件中的代码DragAndDropExampleClass
旨在用作内联代码,它不是实际的类。
因此,您删除了包/类指令。那应该已经解决了您遇到的问题(如评论跟踪中所述)。
然而,在脚本标签中包含内联代码<mx:Script source="DragAndDropExampleClass.as" />
是一种不好的做法,并且可能会导致您的困惑。我已将您的两个文件合并到下面的一个应用程序中。它与 Mike Chambers 的例子完全一样,只是在一个文件中。也许这会有所帮助。我已经运行了这个应用程序并且它可以工作。
注意:我是用 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>