在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檔案,選擇聲明-->“檔案選取器”和”檔案儲存器“-->這兩個都需要選中"支援任何可選類型",如:
最後效果如,如需源碼請點擊 win8Picker.rar 下載: