/// <summary> /// File picker implementation for WPF; uses the Win32 OpenFileDialog from /// PresentationFoundation reference assembly. /// </summary> /// <param name="allowedTypes"> /// Specifies one or multiple allowed types. When null, all file types /// can be selected while picking. /// On WPF, specify strings like this: "Data type (*.ext)|*.ext", which /// corresponds how the Windows file open dialog specifies file types. /// </param> /// <returns>file data of picked file, or null when picking was cancelled</returns> public Task <FileData> PickFile(string[] allowedTypes = null, string defaultName = null, bool saving = false) { var picker = new Gtk.FileChooserDialog( saving ? "Save As" : "Open", null, saving ? FileChooserAction.Save : FileChooserAction.Open, "Cancel", ResponseType.Cancel, saving ? "Save As" : "Open", ResponseType.Accept ); foreach (var type in allowedTypes) { var filter = new FileFilter(); filter.AddMimeType(type); filter.Name = $"{type.Split('/')[0]} files (*.{type.Split('/')[1]})"; picker.AddFilter(filter); } picker.SetCurrentFolder(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); picker.CurrentName = defaultName; var result = picker.Run(); if (result == (int)Gtk.ResponseType.Accept) { FileData data = new PlatformFileData(picker.Filename); picker.Destroy(); return(Task.FromResult(data)); } else { picker.Destroy(); return(Task.FromResult <FileData>(null)); } }
public Task <FileData> PickFile(string[] allowedTypes, string defaultName, bool saving) { // for consistency with other platforms, only allow selecting of a single file. // would be nice if we passed a "file options" to override picking multiple files & directories var openPanel = new NSOpenPanel(); openPanel.CanChooseFiles = true; openPanel.AllowsMultipleSelection = false; openPanel.CanChooseDirectories = false; // macOS allows the file types to contain UTIs, filename extensions or a combination of the two. // If no types are specified, all files are selectable. if (allowedTypes != null) { openPanel.AllowedFileTypes = allowedTypes; } FileData data = null; var result = openPanel.RunModal(); if (result == 1) { // Nab the first file var url = openPanel.Urls[0]; if (url != null) { var path = url.Path; data = new PlatformFileData(path); } } return(Task.FromResult(data)); }