Windows8 FileOpenPicker && FolderPicker
private async void PickAFileButton_Click(object sender, RoutedEventArgs e) { FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.List; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".png"); StorageFile file = await openPicker.PickSingleFileAsync(); if (file != null) { //Application now has read/write access to the picked file using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage();// Set the image source to the selected bitmap bitmapImage.SetSource(fileStream); this.Image.Source = bitmapImage; this.Image.Visibility = Visibility.Visible; } } } private async void PickFilesButton_Click(object sender, RoutedEventArgs e) { FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.List; openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; openPicker.FileTypeFilter.Clear();// Filter to include a sample subset of file types openPicker.FileTypeFilter.Add("*"); IReadOnlyList files = await openPicker.PickMultipleFilesAsync(); if (files.Count > 0) { StringBuilder output = new StringBuilder("Picked Files:\n"); foreach (StorageFile file in files) { output.Append(file.Name + "\n"); } this.OutputTextBlock.Text = output.ToString(); } } private async void PickFolderButton_Click(object sender, RoutedEventArgs e) { // Clear previous returned folder name, if it exists, between iterations of this scenario FolderPicker folderPicker = new FolderPicker(); folderPicker.SuggestedStartLocation = PickerLocationId.Desktop; folderPicker.FileTypeFilter.Add(".docx"); folderPicker.FileTypeFilter.Add(".xlsx"); folderPicker.FileTypeFilter.Add(".pptx"); StorageFolder folder = await folderPicker.PickSingleFolderAsync(); if (folder != null) { //Application now has read/write access to all contents in the picked folder (including other sub-folder contents) StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder); this.OutputTextBlock.Text = "Picked folder: " + folder.Name; } } private async void SaveFileButton_Click(object sender, RoutedEventArgs e) { FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; // Dropdown of file types the user can save the file as savePicker.FileTypeChoices.Add("Plain Text", new List() { ".txt" }); // Default file name if the user does not type one in or select a file to replace savePicker.SuggestedFileName = "New Document"; StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { CachedFileManager.DeferUpdates(file); await FileIO.WriteTextAsync(file, "Hello World"); FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); if (status == FileUpdateStatus.Complete) { this.OutputTextBlock.Text = "File " + file.Name + " was saved."; } else { this.OutputTextBlock.Text = "File " + file.Name + " couldn't be saved."; } } }
posted on 2013-03-27 11:20 JackSlaterYu 阅读(284) 评论(0) 编辑 收藏 举报