Welcome

首页 / 软件开发 / .NET编程技术 / Windows 8 Store Apps学习(25) 选取器: 文件选取窗口等

Windows 8 Store Apps学习(25) 选取器: 文件选取窗口等2013-12-06 cnblogs webabcd选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口

介绍

重新想象 Windows 8 Store Apps 之 选取器

FileOpenPicker - 选择一个文件或多个文件

FolderPicker - 选择一个文件夹

FileSavePicker - 保存文件到指定路径

示例

1、演示如何通过 FileOpenPicker 选择一个文件或多 个文件

Picker/FileOpenPickerDemo.xaml

<Pagex: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 asyncvoid 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 = "取消了";}}}}}