選取器: 檔案選取視窗, 檔案夾選取視窗, 檔案儲存視窗
介紹
重新想象 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 = "取消了"; } } } }}