예제 #1
0
        public async Task <StorageFile?> PickSaveFileAsync(CancellationToken token)
        {
            string commitText = "Save File";

            if (!string.IsNullOrWhiteSpace(_picker.CommitButtonText))
            {
                commitText = _picker.CommitButtonText;
            }

            using FileChooserDialog dialog = new FileChooserDialog(
                      "Save File",
                      GtkHost.Window,
                      FileChooserAction.Save,
                      "Cancel", ResponseType.Cancel,
                      commitText, ResponseType.Accept);

            dialog.SelectMultiple = false;
            dialog.SetFilename(_picker.SuggestedFileName);
            dialog.SetCurrentFolder(PickerHelpers.GetInitialDirectory(_picker.SuggestedStartLocation));

            StorageFile?file = null;

            if (dialog.Run() == (int)ResponseType.Accept)
            {
                if (!File.Exists(dialog.Filename))
                {
                    File.Create(dialog.Filename).Dispose();
                }
                file = await StorageFile.GetFileFromPathAsync(dialog.Filename);
            }


            return(file);
        }
예제 #2
0
        public async Task <StorageFolder?> PickSingleFolderAsync(CancellationToken token)
        {
            string commitText = "Select Folder";

            if (!string.IsNullOrWhiteSpace(_picker.CommitButtonText))
            {
                commitText = _picker.CommitButtonText;
            }

            using FileChooserDialog dialog = new FileChooserDialog(
                      "Select Folder",
                      GtkHost.Window,
                      FileChooserAction.SelectFolder,
                      "Cancel", ResponseType.Cancel,
                      commitText, ResponseType.Accept);

            dialog.SelectMultiple = false;

            if (!_picker.FileTypeFilter.Contains("*"))
            {
                throw new ArgumentNullException();
            }

            dialog.SetCurrentFolder(PickerHelpers.GetInitialDirectory(_picker.SuggestedStartLocation));

            StorageFolder folder = null;

            if (dialog.Run() == (int)ResponseType.Accept)
            {
                folder = await StorageFolder.GetFolderFromPathAsync(dialog.Filename);
            }

            return(folder);
        }
예제 #3
0
        public async Task <StorageFile?> PickSaveFileAsync(CancellationToken token)
        {
            var saveFileDialog = new SaveFileDialog
            {
                CheckPathExists = true,
                OverwritePrompt = true,
            };

            var filterBuilder = new StringBuilder();

            foreach (var choice in _picker.FileTypeChoices)
            {
                if (filterBuilder.Length > 0)
                {
                    filterBuilder.Append('|');
                }

                filterBuilder.Append(choice.Key);
                filterBuilder.Append('|');
                filterBuilder.Append(string.Join(";", choice.Value.Select(item => $"*{item}")));
            }

            saveFileDialog.Filter = filterBuilder.ToString();

            saveFileDialog.FileName         = _picker.SuggestedFileName;
            saveFileDialog.InitialDirectory = PickerHelpers.GetInitialDirectory(_picker.SuggestedStartLocation);

            if (saveFileDialog.ShowDialog() == true)
            {
                return(await StorageFile.GetFileFromPathAsync(saveFileDialog.FileName));
            }
            return(null);
        }
예제 #4
0
        private async Task <StorageFile[]> OpenPickerAsync(bool multiple)
        {
            var openFileDialog = new OpenFileDialog
            {
                Multiselect = multiple
            };

            var filterBuilder = new StringBuilder();

            filterBuilder.Append(string.Join("|", _picker.FileTypeFilter.Select(fileType => $"{fileType}|*{fileType}")));

            if (_picker.FileTypeFilter.Count > 1)
            {
                // Add default entry for all item types at once
                var fullFilter = string.Join(";", _picker.FileTypeFilter.Select(fileType => $"*{fileType}"));
                filterBuilder.Append($"All|{fullFilter}");
            }

            openFileDialog.Filter      = filterBuilder.ToString();
            openFileDialog.FilterIndex = _picker.FileTypeFilter.Count > 1 ? _picker.FileTypeFilter.Count : 0;

            openFileDialog.InitialDirectory = PickerHelpers.GetInitialDirectory(_picker.SuggestedStartLocation);

            var files = new List <StorageFile>();

            if (openFileDialog.ShowDialog() == true)
            {
                foreach (var fileName in openFileDialog.FileNames)
                {
                    files.Add(await StorageFile.GetFileFromPathAsync(fileName));
                }
            }
            return(files.ToArray());
        }
예제 #5
0
        private async Task <StorageFile[]> OpenPickerAsync(bool multiple)
        {
            string commitText = "Open";

            if (!string.IsNullOrWhiteSpace(_picker.CommitButtonText))
            {
                commitText = _picker.CommitButtonText;
            }

            FileChooserDialog dialog = new FileChooserDialog(
                "Open",
                GtkHost.Window,
                FileChooserAction.Open,
                "Cancel", ResponseType.Cancel,
                commitText, ResponseType.Accept);

            dialog.SelectMultiple = multiple;

            if (!_picker.FileTypeFilter.Contains("*"))
            {
                FileFilter filter = new FileFilter();

                foreach (string pattern in _picker.FileTypeFilter)
                {
                    // Pattern is already validated to start with a period, so prepend star
                    filter.AddPattern("*" + pattern);
                }

                if (_picker.FileTypeFilter.Count > 0)
                {
                    dialog.AddFilter(filter);
                }
            }

            dialog.SetCurrentFolder(PickerHelpers.GetInitialDirectory(_picker.SuggestedStartLocation));

            var files = new List <StorageFile>();

            if (dialog.Run() == (int)ResponseType.Accept)
            {
                foreach (var fileName in dialog.Filenames)
                {
                    files.Add(await StorageFile.GetFileFromPathAsync(fileName));
                }
            }

            dialog.Destroy();
            return(files.ToArray());
        }
예제 #6
0
        private async Task <StorageFile[]> OpenPickerAsync(bool multiple)
        {
            var openFileDialog = new OpenFileDialog
            {
                Multiselect = multiple
            };

            var filterBuilder       = new StringBuilder();
            var filterIndex         = -1;
            var fileTypeFilterCount = _picker.FileTypeFilter.Count;

            for (var i = 0; i < fileTypeFilterCount; i++)
            {
                var fileType = _picker.FileTypeFilter[i];
                if (fileType == "*")
                {
                    filterBuilder.Append("|All|*");
                    filterIndex = i + 1;
                }
                else
                {
                    filterBuilder.Append($"|{fileType}|*{fileType}");
                }
            }

            if (filterIndex == -1)
            {
                var fullFilter = string.Join(";", _picker.FileTypeFilter.Select(fileType => $"*{fileType}"));
                filterBuilder.Append($"|All|{fullFilter}");
                filterIndex = fileTypeFilterCount;
            }

            openFileDialog.Filter      = filterBuilder.ToString(1, filterBuilder.Length - 1);
            openFileDialog.FilterIndex = filterIndex;

            openFileDialog.InitialDirectory = PickerHelpers.GetInitialDirectory(_picker.SuggestedStartLocation);

            var files = new List <StorageFile>();

            if (openFileDialog.ShowDialog() == true)
            {
                foreach (var fileName in openFileDialog.FileNames)
                {
                    files.Add(await StorageFile.GetFileFromPathAsync(fileName));
                }
            }
            return(files.ToArray());
        }