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>
도움 주셔서 감사합니다. 이 모든 것이 지금 무엇을 의미하는지 알아봐야 할 것입니다! – Richard
방금 내 대답을 편집했습니다. 첫 번째 것은 정확합니다 (앱이 컴파일되었을 것입니다). 클래스가 EventDispatcher 인 경우 앱이 실행되지 않았을 것입니다. –
내 클래스에서 내 outputField를 식별하는 방법을 알고 계시겠습니까? ID가 "outputField"인 textArea가 주 응용 프로그램 내에 있지만 내 DragAndDropExample 클래스에 등록되어 있지 않습니다. 주요 응용 프로그램은 다음과 같습니다. http://pastebin.com/iYCYuTjh – Richard