在Windows 8中的文件選擇器相比windows之前的幾個版本有很大的不同,在本文中也將講解如何使用 windows文件選擇器進行單選和多選文件,另外也要看看文件保存器和文件夾選擇器。
在這之前我們需 要了解這三種選擇器在Windows 8中所使用的類如下:
文件選擇器:FileOpenPicker
常用屬性 和方法:SuggestedStartLocation-選擇器的初始位置
CommitButtonText-提交按鈕的文字
FileTypeFilter-設置可選擇的文件類型
ViewMode-浏覽模式:縮略圖和文件列表兩種模式
PickSingleFileAsync()-選擇單個文件的方法
PickMultipleFilesAsync()-選擇多個文件的方 法
文件保存器:FileSavePicker
常用屬性和方法:SuggestedStartLocation-選擇器的 初始位置
CommitButtonText-提交按鈕的文字
SuggestedFileName-默認保存文件名稱
DefaultFileExtension-默認的文件類型
FileTypeChoices-設置可選擇的文件類型組合
PickSaveFileAsync()-保存文件的方法
文件選擇器:FolderOpenPicker
常用屬 性和方法:SuggestedStartLocation-選擇器的初始位置
CommitButtonText-提交按鈕的文字
FileTypeFilter-設置可選擇的文件類型
ViewMode-浏覽模式:縮略圖和文件列表兩種模式
PickSingleFolderAsync()-選擇單個文件的方法
下面我們通過一個實例來看如何使用這三種選 擇器:
Xaml:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Button Content="單文件選擇器" HorizontalAlignment="Left" Margin="171,142,0,0" Name="btnSingleFile" VerticalAlignment="Top" Height="55" Width="192" Click="btnSingleFile_Click"/> <Button Content="多文件選擇器" HorizontalAlignment="Left" Margin="501,142,0,0" Name="btnMultipleFile" VerticalAlignment="Top" Height="55" Width="192" Click="btnMultipleFile_Click"/> <Button Content="文件保存器" HorizontalAlignment="Left" Margin="171,250,0,0" Name="btnSaveFile" VerticalAlignment="Top" Height="55" Width="192" Click="btnSaveFile_Click"/> <Button Content="文件夾選擇器" HorizontalAlignment="Left" Margin="501,250,0,0" Name="btnSingleFolder" VerticalAlignment="Top" Height="55" Width="192" Click="btnSingleFolder_Click"/> <TextBlock HorizontalAlignment="Left" Margin="171,44,0,0" TextWrapping="Wrap" Name="tbMsg" Text="TextBlock" VerticalAlignment="Top" Height="34" Width="522" FontSize="18"/> </Grid>
Xaml.cs:
private async void btnSingleFile_Click(object sender, RoutedEventArgs e) { FileOpenPicker filepicker = new FileOpenPicker(); //默認桌面文件夾 filepicker.SuggestedStartLocation = PickerLocationId.Desktop; //提交按鈕的顯示文字 filepicker.CommitButtonText = "確定選擇"; //選擇器可選擇的文件類型 filepicker.FileTypeFilter.Add(".txt"); filepicker.FileTypeFilter.Add(".jpg"); filepicker.FileTypeFilter.Add(".pdf"); filepicker.FileTypeFilter.Add(".bmp"); //選擇器的浏覽模式 filepicker.ViewMode = PickerViewMode.Thumbnail; StorageFile file = await filepicker.PickSingleFileAsync(); if (file != null) { this.tbMsg.Text = "你選擇了'" + file.Name + "'文件"; } } private async void btnMultipleFile_Click(object sender, RoutedEventArgs e) { FileOpenPicker filepicker = new FileOpenPicker(); filepicker.SuggestedStartLocation = PickerLocationId.Desktop; filepicker.FileTypeFilter.Add(".txt"); filepicker.FileTypeFilter.Add(".jpg"); filepicker.FileTypeFilter.Add(".pdf"); filepicker.FileTypeFilter.Add(".bmp"); filepicker.ViewMode = PickerViewMode.Thumbnail; IReadOnlyList<StorageFile> fileList = await filepicker.PickMultipleFilesAsync(); if (fileList != null) { var filenamelist = string.Empty; foreach (StorageFile file in fileList) { filenamelist += file.Name + ","; } this.tbMsg.Text = "你選擇了'" + filenamelist + "'等多個文件"; } } private async void btnSaveFile_Click(object sender, RoutedEventArgs e) { FileSavePicker savepicker = new FileSavePicker(); savepicker.SuggestedStartLocation = PickerLocationId.Desktop; savepicker.CommitButtonText = "保存此文件"; savepicker.DefaultFileExtension = ".jpg"; savepicker.FileTypeChoices.Add("Txt File", new List<string>() { ".txt" }); savepicker.FileTypeChoices.Add("Photo View", new List<string>() { ".jpg", ".pdf" }); savepicker.SuggestedFileName = "Default File Name"; StorageFile file = await savepicker.PickSaveFileAsync(); if (file != null) { CachedFileManager.DeferUpdates(file); await FileIO.WriteTextAsync(file, "這是今天學習的內容,存儲位置!"); this.tbMsg.Text = "你保存了'" + file.Name + "'文件"; FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); } } //單文件夾選擇 private async void btnSingleFolder_Click(object sender, RoutedEventArgs e) { FolderPicker folderpicker = new FolderPicker(); folderpicker.SuggestedStartLocation = PickerLocationId.Desktop; folderpicker.CommitButtonText = "確定文件夾"; folderpicker.FileTypeFilter.Add(".txt"); folderpicker.FileTypeFilter.Add(".jpg"); folderpicker.FileTypeFilter.Add(".pdf"); StorageFolder folder = await folderpicker.PickSingleFolderAsync(); if (folder != null) { StorageApplicationPermissions.FutureAccessList.AddOrReplace("Default Folder", folder); this.tbMsg.Text = "你選擇了'" + folder.Name + "'文件夾"; } }
另外需要雙擊項目中Package.appxmanifest文件,選擇聲明-->“文件選擇器”和”文 件保存器“-->這兩個都需要選中"支持任何可選類型",如下圖:
最後效果如下 圖,如需源碼請下載:http://files.cnblogs.com/chengxingliang/win8Picker.rar