Xamarin NSDraggingDestination

时间:2016-07-06 14:23:40

标签: xamarin xamarin.mac

我是XCode和Xamarin.Mac的新手,想要创建一个可以接受文件丢弃的应用程序。

基本上我希望用户在我的应用程序上拖放文件。

我已经找到了一些关于此的教程,但是无法在Xamarin中完成。

我尝试创建一个名为" DropImageView"的新视图。现在我想实施 NSDraggingSource,NSDraggingDestination和NSPasteboardItemDataProvider

由于这些类不是接口,我问我的视图如何实现这个功能。

1 个答案:

答案 0 :(得分:1)

如果有人从您的应用外部拖动到您的应用中,您可以注册自定义"放弃目标的文件类型"视图(或窗口)允许然后覆盖DraggingEnteredDraggingExited等方法......

实施例

public partial class MyDragTargetView : NSView
{
    public MyDragTargetView (IntPtr handle) : base (handle) { }

    public override void AwakeFromNib()
    {
        base.AwakeFromNib();
        RegisterForDraggedTypes(new string[] { NSPasteboard.NSFilenamesType });
    }

    public override NSDragOperation DraggingEntered(NSDraggingInfo sender)
    {
        Console.WriteLine("User is dragging file(s) in our view");
        // we can look at the sender.DraggingPasteboard to determine condition to return... 
        return NSDragOperation.Copy;
    }

    public override void DraggingEnded(NSDraggingInfo sender)
    {
        Console.WriteLine("User has dragged file(s) in our view, lets do something with them");
        var dragItems = NSString.FromData(sender.DraggingPasteboard.GetDataForType(NSPasteboard.NSFilenamesType), NSStringEncoding.UTF8);
        Console.WriteLine("NSFilenamesType Data: " + dragItems);
    }
}

输出:

User is dragging file(s) in our view
User has dragged file(s) in our view, lets do something with them
NSFilenamesType Data: <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>/Users/sushi/Desktop/DFeedback_FeedBack.png</string>
    <string>/Users/sushi/Desktop/DFeedback_BugFeedBack.png</string>
</array>
</plist>