選取器: 文件選取窗口, 文件夾選取窗口, 文件保存窗口
介紹
重新想象 Windows 8 Store Apps 之 選取器
FileOpenPicker - 選擇一個文件或多個文件
FolderPicker - 選擇一個文件夾
FileSavePicker - 保存文件到指定路徑
示例
1、演示如何通過 FileOpenPicker 選擇一個文件或多 個文件
Picker/FileOpenPickerDemo.xaml
<Page x:Class="XamlDemo.Picker.FileOpenPickerDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Picker" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnPickFile" Content="pick a file" Click="btnPickFile_Click_1" Margin="0 10 0 0" /> <Button Name="btnPickFiles" Content="pick multiple files" Click="btnPickFiles_Click_1" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
Picker/FileOpenPickerDemo.xaml.cs
/* * 演示如何通過 FileOpenPicker 選擇一個文件或多個文件 * * FileOpenPicker - 文件選擇窗口 * ViewMode - 文件選擇窗口的視圖模式,Windows.Storage.Pickers.PickerViewMode 枚舉(List 或 Thumbnail) * SuggestedStartLocation - 文件選擇窗口所顯示的初始路徑,Windows.Storage.Pickers.PickerLocationId 枚舉 * DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary,VideosLibrary * FileTypeFilter - 允許顯示在文件選擇窗口的文件類型集合 * CommitButtonText - 文件選擇窗口的提交按鈕的顯示文本,此按鈕默認顯示的文本為“打開” * PickSingleFileAsync() - 彈出文件選擇窗口,以讓用戶選擇一個文件 * PickMultipleFilesAsync() - 彈出文件選擇窗口,以讓用戶選擇多個文件 */ using System; using System.Collections.Generic; using Windows.Storage; using Windows.Storage.AccessCache; using Windows.Storage.Pickers; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace XamlDemo.Picker { public sealed partial class FileOpenPickerDemo : Page { public FileOpenPickerDemo() { this.InitializeComponent(); } private async void btnPickFile_Click_1(object sender, RoutedEventArgs e) { if (XamlDemo.Common.Helper.EnsureUnsnapped()) { // 選擇一個文件 FileOpenPicker openPicker = new FileOpenPicker(); openPicker.CommitButtonText = "選中此文件"; openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".gif"); openPicker.FileTypeFilter.Add(".png"); // 彈出文件選擇窗口 StorageFile file = await openPicker.PickSingleFileAsync(); // 用戶在“文件選擇窗口”中完成操作後,會返回對應的 StorageFile 對象 if (file != null) { lblMsg.Text = "選中文件: " + file.Name; } else { lblMsg.Text = "取消了"; } } } private async void btnPickFiles_Click_1(object sender, RoutedEventArgs e) { if (XamlDemo.Common.Helper.EnsureUnsnapped()) { // 選擇多個文件 FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.List; openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; openPicker.FileTypeFilter.Add("*"); // 彈出文件選擇窗口 IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync(); // 用戶在“文件選擇窗口”中完成操作後,會返回對應的 StorageFile 對象 if (files.Count > 0) { lblMsg.Text = "選中文件: "; lblMsg.Text += Environment.NewLine; foreach (StorageFile file in files) { lblMsg.Text += (file.Name); lblMsg.Text += Environment.NewLine; } } else { lblMsg.Text = "取消了"; } } } } }
2、演示如何通過 FolderPicker 選擇一個文件夾
Picker/FolderPickerDemo.xaml
<Page x:Class="XamlDemo.Picker.FolderPickerDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Picker" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnPickFolder" Content="pick a folder" Click="btnPickFolder_Click_1" Margin="0 10 0 0 " /> </StackPanel> </Grid> </Page>
Picker/FolderPickerDemo.xaml.cs
/* * 演示如何通過 FolderPicker 選擇一個文件夾 * * FolderPicker - 文件夾選擇窗口 * ViewMode - 文件夾選擇窗口的視圖模式,Windows.Storage.Pickers.PickerViewMode 枚舉(List 或 Thumbnail) * SuggestedStartLocation - 文件夾選擇窗口所顯示的初始路徑,Windows.Storage.Pickers.PickerLocationId 枚舉 * DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary,VideosLibrary * FileTypeFilter - 允許顯示在文件夾選擇窗口的文件類型集合(只能顯示符合要求的文件,但是無法選中) * CommitButtonText - 文件夾選擇窗口的提交按鈕的顯示文本,此按鈕默認顯示的文本為“選擇這個文件夾” * PickSingleFolderAsync() - 彈出文件夾選擇窗口,以讓用戶選擇一個文件夾 */ using System; using Windows.Storage; using Windows.Storage.Pickers; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace XamlDemo.Picker { public sealed partial class FolderPickerDemo : Page { public FolderPickerDemo() { this.InitializeComponent(); } private async void btnPickFolder_Click_1(object sender, RoutedEventArgs e) { if (XamlDemo.Common.Helper.EnsureUnsnapped()) { // 選擇一個文件夾 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(); // 用戶在“文件夾選擇窗口”中完成操作後,會返回對應的 StorageFolder 對象 if (folder != null) { lblMsg.Text = "選中文件夾: " + folder.Name; } else { lblMsg.Text = "取消了"; } } } } }
3、演示如何通過 FileSavePicker 保存文件到指定路徑
Picker/FileSavePickerDemo.xaml
<Page x:Class="XamlDemo.Picker.FileSavePickerDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Pikcer" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnSaveFile" Content="save a file" Click="btnSaveFile_Click_1" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
Picker/FileSavePickerDemo.xaml.cs
/* * 演示如何通過 FileSavePicker 保存文件到指定路徑 * * FileSavePicker - 文件保存窗口 * SuggestedStartLocation - 文件保存窗口所顯示的初始路徑,Windows.Storage.Pickers.PickerLocationId 枚舉 * DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary,VideosLibrary * SuggestedFileName - 需要保存的文件的默認文件名 * SuggestedSaveFile - 需要保存的文件的默認 StorageFile 對象 * FileTypeChoices - 可保存的擴展名集合 * DefaultFileExtension - 默認擴展名 * CommitButtonText - 文件保存窗口的提交按鈕的顯示文本,此按鈕默認顯示的文本為“保存” * PickSaveFileAsync() - 彈出文件保存窗口,以讓用戶保存文件 */ using System; using System.Collections.Generic; using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Provider; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace XamlDemo.Picker { public sealed partial class FileSavePickerDemo : Page { public FileSavePickerDemo() { this.InitializeComponent(); } private async void btnSaveFile_Click_1(object sender, RoutedEventArgs e) { if (XamlDemo.Common.Helper.EnsureUnsnapped()) { FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; // 在擴展名選擇框中將會顯示:文本(.txt) savePicker.FileTypeChoices.Add("文本", new List<string>() { ".txt" }); savePicker.SuggestedFileName = "webabcdFileSavePicker"; // 彈出文件保存窗口 StorageFile file = await savePicker.PickSaveFileAsync(); // 用戶在“文件保存窗口”中完成操作後,會返回對應的 StorageFile 對象 if (file != null) { /* * 運行到此,只是在目標地址創建了一個沒有任何內容的空白文件而已,接下來開始向文件寫入內容 */ // 告訴 Windows ,從此時開始要防止其它程序更新指定的文件 CachedFileManager.DeferUpdates(file); // 將指定的內容保存到指定的文件 string textContent = "I am webabcd"; await FileIO.WriteTextAsync(file, textContent); // 告訴 Windows ,從此時開始允許其它程序更新指定的文件 FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); if (status == FileUpdateStatus.Complete) { lblMsg.Text = "文件 " + file.Name + " 保存成功"; } lblMsg.Text += Environment.NewLine; lblMsg.Text += "FileUpdateStatus: " + status.ToString(); } else { lblMsg.Text = "取消了"; } } } } }
OK
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar