ActionScript 3 AIR将文件拖放到应用程序中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AIR将文件拖放到应用程序中相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
    creationComplete="init()">
        <mx:Script>
                <![CDATA[
                        import flash.desktop.DragActions;
                        import mx.controls.Alert;
                        import mx.controls.Image;
                        import flash.filesystem.File;
                        import flash.desktop.TransferableData;
                        import flash.desktop.TransferableFormats;
                        import flash.events.NativeDragEvent;
                        import flash.desktop.DragManager;
                
                        private function init():void{
                            this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
                            this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
                            this.addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT,onDragExit);
                        }
                       
                public function onDragIn(event:NativeDragEvent):void{
                   DragManager.acceptDragDrop(this);
                }

                public function onDrop(event:NativeDragEvent):void{
                    DragManager.dropAction = DragActions.COPY;
                    var dropfiles:Array = event.transferable.dataForFormat(TransferableFormats.FILE_LIST_FORMAT) as Array;
                                for each (var file:File in dropfiles){
                                    switch (file.extension){   
                                case "png" :
                                        addImage(file.nativePath);
                                        break;
                                case "jpg" :
                                        addImage(file.nativePath);
                                        break;
                                case "gif" :
                                        addImage(file.nativePath);
                                        break;
                                default:
                                   Alert.show("Unmapped Extension");
                            }
                      }
                }
              
                public function onDragExit(event:NativeDragEvent):void{
                    trace("Drag exit event.");
                }
                
                private function addImage(nativePath:String):void{
                        var i:Image = new Image();
                        if(Capabilities.os.search("Mac") >= 0){
                                i.source = "file://" + nativePath;
                        } else {
                                i.source = nativePath;
                        }
                        this.addChild(i);
                }
       
                ]]>
        </mx:Script>
</mx:WindowedApplication>

以上是关于ActionScript 3 AIR将文件拖放到应用程序中的主要内容,如果未能解决你的问题,请参考以下文章

有啥方法可以将 ActionScript 3 (Flex/AIR) 项目打印到标准输出?

ActionScript 3 as3 air for android将应用程序文件复制到android sdcard中

ActionScript 3 忽略AIR中的隐藏文件

ActionScript 3 Adobe AIR - 加载本地文本文件

ActionScript 3 适用于Android的Adobe Air文件配置

Xcode Swift MacOS App,将文件拖放到 NSTextField