Welcome

首页 / 软件开发 / .NET编程技术 / Windows 8 Store Apps学习(26) 选取器: 自定义文件选取窗口和保存窗口

Windows 8 Store Apps学习(26) 选取器: 自定义文件选取窗口和保存窗口2013-12-06 cnblogs webabcd选取器: 自定义文件选取窗口, 自定义文件保存窗口

介绍

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

FileOpenPickerUI - 自定义文件打开选取器

FileSavePickerUI - 自定义文件保存选取器

示例

1、 开发一个自定义文件选取窗口。注:如果需要 激活自定义的文件选取窗口,请在弹出的选取器窗口的左上角选择对应 Provider

Picker/MyOpenPicker.xaml

<Pagex:Class="XamlDemo.Picker.MyOpenPicker"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="btnPickLocalFile" Content="选择一个本地文件" Click="btnPickLocalFile_Click_1" Margin="0 10 0 0" /><Button Name="btnPickRemoteFile" Content="选择一个远程文件" Click="btnPickRemoteFile_Click_1" Margin="0 10 0 0" /></StackPanel></Grid></Page>
Picker/MyOpenPicker.xaml.cs

/* * 演示如何开发自定义文件打开选取器 ** 1、在 Package.appxmanifest 中新增一个“文件打开选取器”声明,并做相关配置 * 2、在 App.xaml.cs 中 override void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs args),如果 app 是由文件打开选取器激活的,则可以在此获取其相关信息 ** FileOpenPickerActivatedEventArgs - 通过“文件打开选取器”激活应用程序时的事件参数 * FileOpenPickerUI - 获取 FileOpenPickerUI 对象 * PreviousExecutionState, Kind, SplashScreen - 各种激活 app 的方式的事件参数基本上都有这些属性,就不多说了 ** FileOpenPickerUI - 自定义文件打开选取器的帮助类 * AllowedFileTypes - 允许的文件类型,只读 * SelectionMode - 选择模式(FileSelectionMode.Single 或 FileSelectionMode.Multiple) * Title - 将在“自定义文件打开选取器”上显示的标题 * CanAddFile(IStorageFile file) - 是否可以将指定的文件添加进选中文件列表 * AddFile(string id, IStorageFile file) - 将文件添加进选中文件列表,并指定 id * ContainsFile(string id) - 选中文件列表中是否包含指定的 id * RemoveFile(string id) - 根据 id 从选中文件列表中删除对应的文件 * FileRemoved - 从选中文件列表中删除文件时触发的事件 * Closing - 用户关闭“自定义文件打开选取器”时触发的事件 */using System;using Windows.ApplicationModel;using Windows.ApplicationModel.Activation;using Windows.Storage;using Windows.Storage.Pickers.Provider;using Windows.Storage.Provider;using Windows.Storage.Streams;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation;namespace XamlDemo.Picker{public sealed partial class MyOpenPicker : Page{private FileOpenPickerUI _fileOpenPickerUI;public MyOpenPicker(){this.InitializeComponent();}protected override void OnNavigatedTo(NavigationEventArgs e){// 获取 FileOpenPickerUI 对象var args = (FileOpenPickerActivatedEventArgs)e.Parameter;_fileOpenPickerUI = args.FileOpenPickerUI;_fileOpenPickerUI.Title = "自定义文件打开选取器";// _fileOpenPickerUI.Closing += _fileOpenPickerUI_Closing;// _fileOpenPickerUI.FileRemoved += _fileOpenPickerUI_FileRemoved;}protected override void OnNavigatedFrom(NavigationEventArgs e){// _fileOpenPickerUI.Closing -= _fileOpenPickerUI_Closing;// _fileOpenPickerUI.FileRemoved -= _fileOpenPickerUI_FileRemoved;}// 选择一个本地文件private async void btnPickLocalFile_Click_1(object sender, Windows.UI.Xaml.RoutedEventArgs e){/* * 注意: * 1、选择的文件的扩展名必须匹配 FileOpenPicker.FileTypeFilter 中的定义 * 2、如果通过 KnownFolders.DocumentsLibrary 等选择文件,除了要在 Package.appxmanifest 选择对应的“库”功能外,还必须在 Package.appxmanifest 中的“文件类型关联”声明中增加对相应的的扩展名的支持 */StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"AssetsLogo.png");AddFileResult result = _fileOpenPickerUI.AddFile("myFile", file);lblMsg.Text = "选择的文件: " + file.Name;lblMsg.Text += Environment.NewLine;lblMsg.Text += "AddFileResult: " + result.ToString();}// 选择一个远程文件private async void btnPickRemoteFile_Click_1(object sender, Windows.UI.Xaml.RoutedEventArgs e){Uri uri = new Uri("http://images.cnblogs.com/mvpteam.gif", UriKind.Absolute);// 扩展名必须匹配 FileOpenPicker.FileTypeFilter 中的定义StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync("mvp.gif", uri, RandomAccessStreamReference.CreateFromUri(uri));AddFileResult result = _fileOpenPickerUI.AddFile("myFile", file);lblMsg.Text = "选择的文件: " + file.Name;lblMsg.Text += Environment.NewLine;lblMsg.Text += "AddFileResult: " + result.ToString();}}}
判断程序是否是由文件打开选取器激活,在 App.xaml.cs 中 override void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs args)

// 通过文件打开选取器激活应用程序时所调用的方法protected override void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs args){var rootFrame = new Frame();rootFrame.Navigate(typeof(XamlDemo.Picker.MyOpenPicker), args);Window.Current.Content = rootFrame;Window.Current.Activate();}
2、 开发一个自定义文件保存窗口。注:如果需要激活自定义的文件保存窗口,请在弹出的选取器 窗口的左上角选择对应 Provider
Picker/MySavePicker.xaml

<Pagex:Class="XamlDemo.Picker.MySavePicker"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" /></StackPanel></Grid></Page>