Search code examples
c#viewcontrollervisual-studio-macxamarin.mac

Xamarin.Mac dock icon does not accept drag and drop files


My application does not accept drag and drop files on the Dock icons like other applications.

enter image description here

For example, the dock icon in Visual Studio for Mac accepts the file, but when I build and run the default project, it does not accept the drag and drop of the dock icon.

Perhaps I need to configure something in Xcode, but how can I do that?

Also, what kind of code should I write to receive the file path dragged and dropped to the dock icon in ViewController?


Solution

  • I solved this problem myself.

    First, open the info.plist in your project and click the Advanced tab in the bottom of window, and then Add Document Type button.

    enter image description here

    Select Document in Class item, and for Identifier, enter public.item. For Role, set it to None.

    enter image description here

    Then you will be able to drag and drop it to the Dock icon.

    enter image description here

    Then write the code can receive the dragged and dropped files in the program side.

    AppDelegate.cs
    public override void OpenUrls(NSApplication application, NSUrl[] urls)
    {
      //base.OpenUrls(application, urls);
      var alert = new NSAlert();
      alert.MessageText = "AppDelegate.OpenUrls()";
      alert.InformativeText = urls[0].AbsoluteString;
      alert.AddButton("Ok");
      alert.RunModal();
    }
    

    ref. https://forums.xamarin.com/discussion/158538/xamarin-mac-appdelegate-openurls-not-invoked

    Now you can receive and view the information about the dragged and dropped files.

    enter image description here