public static async Task <StorageFile> DisplaySingleFilePicker( PickerViewMode viewMode = PickerViewMode.List, PickerLocationId preferLocation = PickerLocationId.Desktop, List <string> filters = null) { var filePicker = new FileOpenPicker { ViewMode = viewMode, SuggestedStartLocation = preferLocation }; if (filters is null) { filters = new List <string> { "*" }; } foreach (var filter in filters) { filePicker.FileTypeFilter.Add(filter); } StorageFile file = await filePicker.PickSingleFileAsync(); return(file); }
public async static Task <StorageFile> PickFiles(List <string> fileTypes, string metaData = "New Storage Item", PickerViewMode pickerViewMode = PickerViewMode.Thumbnail, PickerLocationId pickerLocationId = PickerLocationId.PicturesLibrary) { var picker = new FileOpenPicker(); picker.ViewMode = pickerViewMode; picker.SuggestedStartLocation = pickerLocationId; foreach (string type in fileTypes) { picker.FileTypeFilter.Add(type); } StorageFile file = await picker.PickSingleFileAsync(); if (file != null) { // Application now has read/write access to the picked file return(file); } Console.WriteLine("Operation cancelled."); return(null); }
public static async Task <StorageFolder> PickFolderAsync(PickerViewMode viewMode = PickerViewMode.List, PickerLocationId suggestedStartLocation = PickerLocationId.ComputerFolder) { var folderPicker = new FolderPicker() { ViewMode = viewMode, SuggestedStartLocation = suggestedStartLocation, }; folderPicker.FileTypeFilter.Add("*"); var folder = await folderPicker.PickSingleFolderAsync(); return(folder); }
public static async Task <StorageFolder> DisplayFolderPicker( PickerViewMode viewMode = PickerViewMode.List, PickerLocationId preferLocation = PickerLocationId.Desktop) { var folderPicker = new FolderPicker() { ViewMode = viewMode, SuggestedStartLocation = preferLocation }; folderPicker.FileTypeFilter.Add("*"); StorageFolder folder = await folderPicker.PickSingleFolderAsync(); return(folder); }
public static async Task <StorageFile> PickFileAsync(PickerViewMode viewMode = PickerViewMode.List, PickerLocationId suggestedStartLocation = PickerLocationId.ComputerFolder, params string[] desiredExtensions) { var picker = new FileOpenPicker { ViewMode = viewMode, SuggestedStartLocation = suggestedStartLocation, }; if (desiredExtensions != null && desiredExtensions.Length > 0) { foreach (var extension in desiredExtensions) { picker.FileTypeFilter.Add(extension); } } else { picker.FileTypeFilter.Add("*"); } var file = await picker.PickSingleFileAsync(); return(file); }
public static async Task <StorageFile> ShowLoadFilePicker(string[] filters = null, PickerLocationId startLocation = PickerLocationId.PicturesLibrary, PickerViewMode viewMode = PickerViewMode.Thumbnail, bool addToRecent = false, bool addToFuture = false) { var picker = new FileOpenPicker(); picker.SuggestedStartLocation = startLocation; picker.ViewMode = viewMode; if (filters != null) { foreach (string filter in filters) { picker.FileTypeFilter.Add(filter); } } StorageFile file = await picker.PickSingleFileAsync(); if (file != null) { if (addToRecent) { StorageApplicationPermissions.MostRecentlyUsedList.Add(file); } if (addToFuture) { StorageApplicationPermissions.FutureAccessList.Add(file); } return(file); } else { return(null); } }
public static FileOpenPicker GetFileOpenPicker(List <string> fileTypes, PickerLocationId locationId = PickerLocationId.DocumentsLibrary, PickerViewMode viewMode = PickerViewMode.Thumbnail) { FileOpenPicker fileOpenPicker = new FileOpenPicker { SuggestedStartLocation = locationId, ViewMode = viewMode }; fileTypes.ForEach((fileType) => fileOpenPicker.FileTypeFilter.Add(fileType)); return(fileOpenPicker); }
/// <summary> /// Displays a folder browser dialog to the user. /// </summary> /// <param name="commitButtonText">The text, that is displayed on the commit button, that the user has to press to select a folder.</param> /// <param name="suggestedStartLocation">The suggested start location, which is initally displayed by the browse folder dialog.</param> /// <param name="viewMode">The view mode, which determines whether the browse folder dialog displays the folders as a list or as thumbnails.</param> /// <returns>Returns the dialog result and the folder that was selected by the user.</returns> public virtual async Task <DialogResult <StorageFolder> > ShowFolderBrowseDialogAsync(string commitButtonText, PickerLocationId suggestedStartLocation, PickerViewMode viewMode) { // Creates a new task completion source for the result of the folder browser dialog TaskCompletionSource <StorageFolder> taskCompletionSource = new TaskCompletionSource <StorageFolder>(); // Executes the folder browser dialog on the dispatcher thread of the current core application view, this is needed so that the code is always executed on the UI thread await CoreApplication.GetCurrentView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { // Creates a new browse folder dialog FolderPicker folderPicker = new FolderPicker { CommitButtonText = commitButtonText, SuggestedStartLocation = suggestedStartLocation, ViewMode = viewMode }; // Shows the browse folder dialog and stores the result taskCompletionSource.TrySetResult(await folderPicker.PickSingleFolderAsync()); }); // Checks if the user cancelled the browse folder dialog, if so then cancel is returned, otherwise okay is returned with the folder that was picked by the user StorageFolder storageFolder = await taskCompletionSource.Task; if (storageFolder == null) { return(new DialogResult <StorageFolder>(DialogResult.Cancel, null)); } else { return(new DialogResult <StorageFolder>(DialogResult.Okay, storageFolder)); } }
/// <summary> /// 写真を選択します。 /// </summary> /// <param name="viewMode">ファイル オープン ピッカーが項目を表示するために使用する表示モード。</param> /// <returns></returns> #if WINDOWS_APP private async static Task<StorageFile> PickPhotoAsync(PickerViewMode viewMode)
/// <summary> /// Displays a file open dialog to the user. /// </summary> /// <param name="commitButtonText">The text, that is displayed on the commit button, that the user has to press to select one or multiple files.</param> /// <param name="fileTypeFilter">The file type filter, which determines which kinds of files are displayed to the user.</param> /// <param name="suggestedStartLocation">The suggested start location, which is initally displayed by the open file dialog.</param> /// <param name="viewMode">The view mode, which determines whether the open file dialog displays the files as a list or as thumbnails.</param> /// <param name="isMultiselect">Determines whether the user is able to select multiple files.</param> /// <returns>Returns the dialog result and the files that were selected by the user.</returns> public virtual async Task <DialogResult <IEnumerable <StorageFile> > > ShowOpenFileDialogAsync(string commitButtonText, IEnumerable <string> fileTypeFilter, PickerLocationId suggestedStartLocation, PickerViewMode viewMode, bool isMultiselect) { // Creates a new task completion source for the result of the open file dialog TaskCompletionSource <IEnumerable <StorageFile> > taskCompletionSource = new TaskCompletionSource <IEnumerable <StorageFile> >(); // Executes the open file dialog on the dispatcher thread of the current core application view, this is needed so that the code is always executed on the UI thread await CoreApplication.GetCurrentView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { // Creates a new open file dialog FileOpenPicker fileOpenPicker = new FileOpenPicker { CommitButtonText = commitButtonText, SuggestedStartLocation = suggestedStartLocation, ViewMode = viewMode }; foreach (string currentFileTypeFilter in fileTypeFilter) { fileOpenPicker.FileTypeFilter.Add(currentFileTypeFilter == "*" ? currentFileTypeFilter : $".{currentFileTypeFilter}"); } // Shows the open file dialog and stores the result if (isMultiselect) { taskCompletionSource.TrySetResult(await fileOpenPicker.PickMultipleFilesAsync()); } else { taskCompletionSource.TrySetResult(new List <StorageFile> { await fileOpenPicker.PickSingleFileAsync() }); } }); // Checks if the user cancelled the open file dialog, if so then cancel is returned, otherwise okay is returned with the files that were picked by the user IEnumerable <StorageFile> storageFiles = await taskCompletionSource.Task; if (storageFiles == null || !storageFiles.Any() || storageFiles.Any(storageFile => storageFile == null)) { return(new DialogResult <IEnumerable <StorageFile> >(DialogResult.Cancel, null)); } else { return(new DialogResult <IEnumerable <StorageFile> >(DialogResult.Okay, storageFiles)); } }
public static async Task <StorageFolder> PickSingleFolderAsync(List <string> fileTypes, PickerLocationId startLocation = PickerLocationId.Desktop, PickerViewMode viewMode = PickerViewMode.Thumbnail) { StorageFolder pickedFolder = null; if (!isPickerOpened) { isPickerOpened = true; FolderPicker folderPicker = new FolderPicker(); folderPicker.SuggestedStartLocation = startLocation; folderPicker.ViewMode = viewMode; foreach (var format in fileTypes) { folderPicker.FileTypeFilter.Add(format); } try { pickedFolder = await folderPicker.PickSingleFolderAsync(); } catch (UnauthorizedAccessException) { } finally { RaisePickerOpened(); } isPickerOpened = false; } return(pickedFolder); }
/// <summary> /// Displays a folder browser dialog to the user. /// </summary> /// <param name="commitButtonText">The text, that is displayed on the commit button, that the user has to press to select a folder.</param> /// <param name="suggestedStartLocation">The suggested start location, which is initally displayed by the browse folder dialog.</param> /// <param name="viewMode">The view mode, which determines whether the browse folder dialog displays the folders as a list or as thumbnails.</param> /// <returns>Returns the dialog result and the folder that was selected by the user.</returns> public virtual async Task<DialogResult<StorageFolder>> ShowFolderBrowseDialogAsync(string commitButtonText, PickerLocationId suggestedStartLocation, PickerViewMode viewMode) { // Creates a new task completion source for the result of the folder browser dialog TaskCompletionSource<StorageFolder> taskCompletionSource = new TaskCompletionSource<StorageFolder>(); // Executes the folder browser dialog on the dispatcher thread of the current core application view, this is needed so that the code is always executed on the UI thread await CoreApplication.GetCurrentView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { // Creates a new browse folder dialog FolderPicker folderPicker = new FolderPicker { CommitButtonText = commitButtonText, SuggestedStartLocation = suggestedStartLocation, ViewMode = viewMode }; // Shows the browse folder dialog and stores the result taskCompletionSource.TrySetResult(await folderPicker.PickSingleFolderAsync()); }); // Checks if the user cancelled the browse folder dialog, if so then cancel is returned, otherwise okay is returned with the folder that was picked by the user StorageFolder storageFolder = await taskCompletionSource.Task; if (storageFolder == null) return new DialogResult<StorageFolder>(DialogResult.Cancel, null); else return new DialogResult<StorageFolder>(DialogResult.Okay, storageFolder); }
/// <summary> /// Displays a file open dialog to the user. /// </summary> /// <param name="commitButtonText">The text, that is displayed on the commit button, that the user has to press to select a file.</param> /// <param name="fileTypeFilter">The file type filter, which determines which kinds of files are displayed to the user.</param> /// <param name="suggestedStartLocation">The suggested start location, which is initally displayed by the open file dialog.</param> /// <param name="viewMode">The view mode, which determines whether the open file dialog displays the files as a list or as thumbnails.</param> /// <returns>Returns the dialog result and the file that was selected by the user.</returns> public virtual async Task<DialogResult<StorageFile>> ShowOpenFileDialogAsync(string commitButtonText, IEnumerable<string> fileTypeFilter, PickerLocationId suggestedStartLocation, PickerViewMode viewMode) { // Shows the open file dialog to the user DialogResult<IEnumerable<StorageFile>> dialogResult = await this.ShowOpenFileDialogAsync(commitButtonText, fileTypeFilter, suggestedStartLocation, viewMode, false); // Checks if the user picked a file, in that case the first one is returned if (dialogResult.Result == DialogResult.Cancel) return new DialogResult<StorageFile>(DialogResult.Cancel, null); else return new DialogResult<StorageFile>(DialogResult.Okay, dialogResult.ResultValue.FirstOrDefault()); }
/// <summary> /// Displays a file open dialog to the user. /// </summary> /// <param name="commitButtonText">The text, that is displayed on the commit button, that the user has to press to select one or multiple files.</param> /// <param name="fileTypeFilter">The file type filter, which determines which kinds of files are displayed to the user.</param> /// <param name="suggestedStartLocation">The suggested start location, which is initally displayed by the open file dialog.</param> /// <param name="viewMode">The view mode, which determines whether the open file dialog displays the files as a list or as thumbnails.</param> /// <param name="isMultiselect">Determines whether the user is able to select multiple files.</param> /// <returns>Returns the dialog result and the files that were selected by the user.</returns> public virtual async Task<DialogResult<IEnumerable<StorageFile>>> ShowOpenFileDialogAsync(string commitButtonText, IEnumerable<string> fileTypeFilter, PickerLocationId suggestedStartLocation, PickerViewMode viewMode, bool isMultiselect) { // Creates a new task completion source for the result of the open file dialog TaskCompletionSource<IEnumerable<StorageFile>> taskCompletionSource = new TaskCompletionSource<IEnumerable<StorageFile>>(); // Executes the open file dialog on the dispatcher thread of the current core application view, this is needed so that the code is always executed on the UI thread await CoreApplication.GetCurrentView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { // Creates a new open file dialog FileOpenPicker fileOpenPicker = new FileOpenPicker { CommitButtonText = commitButtonText, SuggestedStartLocation = suggestedStartLocation, ViewMode = viewMode }; foreach (string currentFileTypeFilter in fileTypeFilter) fileOpenPicker.FileTypeFilter.Add(currentFileTypeFilter == "*" ? currentFileTypeFilter : $".{currentFileTypeFilter}"); // Shows the open file dialog and stores the result if (isMultiselect) taskCompletionSource.TrySetResult(await fileOpenPicker.PickMultipleFilesAsync()); else taskCompletionSource.TrySetResult(new List<StorageFile> { await fileOpenPicker.PickSingleFileAsync() }); }); // Checks if the user cancelled the open file dialog, if so then cancel is returned, otherwise okay is returned with the files that were picked by the user IEnumerable<StorageFile> storageFiles = await taskCompletionSource.Task; if (storageFiles == null || !storageFiles.Any() || storageFiles.Any(storageFile => storageFile == null)) return new DialogResult<IEnumerable<StorageFile>>(DialogResult.Cancel, null); else return new DialogResult<IEnumerable<StorageFile>>(DialogResult.Okay, storageFiles); }
/// <param name="operationName">アプリがアクティブ化されたときに、コンテキストを提供するために操作を特定する名前。</param> private static void PickPhoto(PickerViewMode viewMode, string operationName) #endif { var picker = new FileOpenPicker { ViewMode = viewMode, SuggestedStartLocation = PickerLocationId.PicturesLibrary }; foreach (var extension in SupportedExtensions) { picker.FileTypeFilter.Add(extension); } #if WINDOWS_APP return await picker.PickSingleFileAsync(); #else ContinuationManager.Current.MarkAsStale(); picker.ContinuationData["Operation"] = operationName; picker.PickSingleFileAndContinue(); #endif }
/// <summary> /// Displays a file open dialog to the user. /// </summary> /// <param name="commitButtonText">The text, that is displayed on the commit button, that the user has to press to select a file.</param> /// <param name="fileTypeFilter">The file type filter, which determines which kinds of files are displayed to the user.</param> /// <param name="suggestedStartLocation">The suggested start location, which is initally displayed by the open file dialog.</param> /// <param name="viewMode">The view mode, which determines whether the open file dialog displays the files as a list or as thumbnails.</param> /// <returns>Returns the dialog result and the file that was selected by the user.</returns> public virtual async Task <DialogResult <StorageFile> > ShowOpenFileDialogAsync(string commitButtonText, IEnumerable <string> fileTypeFilter, PickerLocationId suggestedStartLocation, PickerViewMode viewMode) { // Shows the open file dialog to the user DialogResult <IEnumerable <StorageFile> > dialogResult = await this.ShowOpenFileDialogAsync(commitButtonText, fileTypeFilter, suggestedStartLocation, viewMode, false); // Checks if the user picked a file, in that case the first one is returned if (dialogResult.Result == DialogResult.Cancel) { return(new DialogResult <StorageFile>(DialogResult.Cancel, null)); } else { return(new DialogResult <StorageFile>(DialogResult.Okay, dialogResult.ResultValue.FirstOrDefault())); } }
/// <summary> /// アクションを実行します。 /// </summary> /// <param name="viewMode">ファイル オープン ピッカーが項目を表示するために使用する表示モード。</param> /// <param name="callbakCommand">アクションの実行後に呼び出す <see cref="ICommand"/>。</param> /// /// <param name="errorHandleCommand">例外発生時に呼び出す <see cref="ICommand"/>。</param> /// <returns></returns> private async static Task ExecuteAsync(PickerViewMode viewMode, ICommand callbakCommand, ICommand errorHandleCommand) { StorageFile photo; try { photo = await PickPhotoAsync(viewMode); } catch (Exception ex) { if (errorHandleCommand == null) throw; if (!errorHandleCommand.CanExecute(ex)) throw; errorHandleCommand.Execute(ex); return; } if (callbakCommand.CanExecute(photo)) { callbakCommand.Execute(photo); } }