xamarinmonomacxamarin.mac

Xamarin.Mac Unified API and System.Windows.Forms


I recently upgraded to the Xamarin.Mac Unified API. Before the upgrade I was using a FolderBrowserDialog by means of System.Windows.Forms. By upgrading I no longer have access to the System.Windows.Forms namespace. I contacted customer support and they suggested referencing the native APIs. So I added this nuget with its dependencies. The presence of that nuget took away the intellisense errors for the following code:

   var dir = Environment.SpecialFolder.MyPictures.ToString ();

   var dlg = new CommonOpenFileDialog ();
   dlg.Title = "Exclude Subdirectories";
   dlg.IsFolderPicker = true;
   dlg.InitialDirectory = dir;
   dlg.AddToMostRecentlyUsedList = false;
   dlg.AllowNonFileSystemItems = false;
   dlg.DefaultDirectory = dir;
   dlg.EnsureFileExists = true;
   dlg.EnsurePathExists = true;
   dlg.EnsureReadOnly = false;
   dlg.EnsureValidNames = true;
   dlg.Multiselect = false;
   dlg.ShowPlacesList = true;

   if (dlg.ShowDialog () == CommonFileDialogResult.Ok) {
      var folder = dlg.FileName;
   }

But at runtime it busts citing a System.DllNotFoundException for ole32.dll. Any ideas of how to overcome this? This link doesn't make it sound hopeful. My only option at this point is to revert back to the classic api...and if I do that I don't believe it can ever be put in the app store.


Solution

  • No need for that nuget at all. You can accomplish a FolderBrowserDialog with this code:

    NSOpenPanel openDlg = NSOpenPanel.OpenPanel;
    openDlg.Prompt = "Select Directory"; //Defaults to OPEN
    openDlg.CanChooseDirectories = true;
    openDlg.CanChooseFiles = false;
    openDlg.Title = "Choose Monitored Directory";
    openDlg.AllowsMultipleSelection = false;
    openDlg.CanCreateDirectories = true;
    openDlg.ReleasedWhenClosed = true;
    
    openDlg.DirectoryUrl = new NSUrl (Environment.GetFolderPath (System.Environment.SpecialFolder.UserProfile));
    openDlg.Begin (result => {
        var url = openDlg.Url;
    
        if (result == 1) {
            //directory selected
        } else {
            //cancelled
        }
    });