Xamarin.Mac 统一 API 和 System.Windows.Forms

Xamarin.Mac Unified API and System.Windows.Forms

我最近升级到 Xamarin.Mac 统一 API。在升级之前,我通过 System.Windows.Forms 使用 FolderBrowserDialog。通过升级,我无法再访问 System.Windows.Forms 命名空间。我联系了客户支持,他们建议参考原生 APIs。所以我添加了这个 nuget 及其依赖项。该 nuget 的存在消除了以下代码的智能感知错误:

   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;
   }

但在运行时它会为 ole32.dll 引用 System.DllNotFoundException。关于如何克服这个问题的任何想法?这 link 并没有让它听起来充满希望。在这一点上,我唯一的选择是恢复到经典 api...如果我这样做,我认为它永远不会被放入应用程序商店。

根本不需要那个 nuget。您可以使用以下代码完成 FolderBrowserDialog:

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
    }
});