Xamarin NSDraggingDestination

Xamarin NSDraggingDestination

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

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

我找到了一些这方面的教程,但无法在 Xamarin 中完成它。

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

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

如果有人从您的应用程序外部拖入您的应用程序,您可以注册您的自定义 "drop target" 视图(或 window)允许的文件类型,然后重写 DraggingEntered, DraggingExited, 等等...

示例:

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>