原文:重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口

[源码下载]

重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口

作者:webabcd

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

  • FileOpenPickerUI - 自定义文件打开选取器
  • FileSavePickerUI - 自定义文件保存选取器

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

<Page
x: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(@"Assets\Logo.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

<Page
x: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>

Picker/MySavePicker.xaml.cs

/*
* 演示如何开发自定义文件保存选取器
*
* 1、在 Package.appxmanifest 中新增一个“文件保存选取器”声明,并做相关配置
* 2、在 App.xaml.cs 中 override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args),如果 app 是由文件保存选取器激活的,则可以在此获取其相关信息
*
* FileSavePickerActivatedEventArgs - 通过“文件保存选取器”激活应用程序时的事件参数
* FileSavePickerUI - 获取 FileSavePickerUI 对象
* PreviousExecutionState, Kind, SplashScreen - 各种激活 app 的方式的事件参数基本上都有这些属性,就不多说了
*
* FileSavePickerUI - 自定义文件保存选取器的帮助类
* AllowedFileTypes - 允许的文件类型,只读
* Title - 将在“自定义文件保存选取器”上显示的标题
* FileName - 需要保存的文件名(包括文件名和扩展名),只读
* TrySetFileName(string value) - 尝试指定需要保存的文件名(包括文件名和扩展名)
* FileNameChanged - 用户在文件名文本框中更改文件名或在文件类型下拉框中更改扩展名时触发的事件
* TargetFileRequested - 用户提交保存时触发的事件(事件参数:TargetFileRequestedEventArgs)
*
* TargetFileRequestedEventArgs
* Request - 返回 TargetFileRequest 对象
*
* TargetFileRequest
* TargetFile - 目标文件对象,用于返回给 client
* GetDeferral() - 获取异步操作对象,同时开始异步操作,之后通过 Complete() 通知完成异步操作
*/ using System;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
using Windows.Storage.Pickers.Provider;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace XamlDemo.Picker
{
public sealed partial class MySavePicker : Page
{
private FileSavePickerUI _fileSavePickerUI; public MySavePicker()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 获取 FileSavePickerUI 对象
var args = (FileSavePickerActivatedEventArgs)e.Parameter;
_fileSavePickerUI = args.FileSavePickerUI; _fileSavePickerUI.Title = "自定义文件保存选取器"; _fileSavePickerUI.TargetFileRequested += _fileSavePickerUI_TargetFileRequested;
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
_fileSavePickerUI.TargetFileRequested -= _fileSavePickerUI_TargetFileRequested;
} private async void _fileSavePickerUI_TargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs args)
{
// 异步操作
var deferral = args.Request.GetDeferral(); try
{
// 在指定的地址新建一个没有任何内容的空白文件
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(sender.FileName, CreationCollisionOption.GenerateUniqueName); // 设置 TargetFile,“自定义文件保存选取器”的调用端会收到此对象
args.Request.TargetFile = file;
}
catch (Exception ex)
{
// 输出异常信息
OutputMessage(ex.ToString());
}
finally
{
// 完成异步操作
deferral.Complete();
}
} private async void OutputMessage(string msg)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
lblMsg.Text = msg;
});
}
}
}

判断程序是否是由文件保存选取器激活,在 App.xaml.cs 中 override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args)

// 通过文件保存选取器激活应用程序时所调用的方法
protected override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args)
{
var rootFrame = new Frame();
rootFrame.Navigate(typeof(XamlDemo.Picker.MySavePicker), args);
Window.Current.Content = rootFrame; Window.Current.Activate();
}

OK
[源码下载]

重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口的更多相关文章

  1. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

  2. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  3. 重新想象 Windows 8 Store Apps (28) - 选取器: CachedFileUpdater(缓存文件更新程序)

    原文:重新想象 Windows 8 Store Apps (28) - 选取器: CachedFileUpdater(缓存文件更新程序) [源码下载] 重新想象 Windows 8 Store App ...

  4. 重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口

    原文:重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口 [源码下载] 重新想象 Windows 8 Store Apps (27) - ...

  5. 重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口

    原文:重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (2 ...

  6. 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract

    [源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  7. 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化

    [源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  8. 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu

    [源码下载] 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu 作者 ...

  9. 重新想象 Windows 8 Store Apps (19) - 动画: 线性动画, 关键帧动画, 缓动动画

    原文:重新想象 Windows 8 Store Apps (19) - 动画: 线性动画, 关键帧动画, 缓动动画 [源码下载] 重新想象 Windows 8 Store Apps (19) - 动画 ...

随机推荐

  1. 近期在调用 calendar.js 的时候出现中文乱码! 解决方式

    近期写一个小项目的时候:在调用 calendar.js  的时候出现中文乱码! 如图所看到的: 原因在于: 我的jsp 页面,指定的是 UTF-8 编码,然而,调用的 calendar.js 的编码确 ...

  2. HNCU1101:马的移动(BFS)

    http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1101 题目描述 小明很喜欢下国际象棋,一 ...

  3. hprose rpc使用实例(同时有Java和Delphi客户端的例子)

    php server <?php require_once('src/Hprose.php'); function hello($name) { echo "Hello $name!& ...

  4. 无法引入import com.sun.management.OperatingSystemMXBean

    现象:在JDK的安装包的jre\lib\rt.jar包里确实有这个类com.sun.management.OperatingSystemMXBean,但是就是不能import  com.sun.man ...

  5. 百度地图之UI控制

    在本文中主要介绍百度地图UI控制功能,即控制地图是否有缩放.平移.双击放大.旋转.俯视的功能以及控制是否显示内置缩放组件.指南针位置等.在文中采用标签监听使每个控制功能的方法见名知义,代码原型来源百度 ...

  6. 做外贸,独立B2C商城好,还是平台好

    随着跨境电商热的来临,越来越多的国内企业选择进军跨国电商,那么企业要想进军以互联网跨国销售,通常会通过两种途径,一种是建立独立运营的B2C商城,还有一种是依托alibaba,dhgate,aliexp ...

  7. brutal是什么意思_brutal在线翻译_英语_读音_用法_例句_海词词典

    brutal是什么意思_brutal在线翻译_英语_读音_用法_例句_海词词典 brutal

  8. HDU 4022 Bombing (map + multiset)

    题意: 在x,y坐标范围为10 ^ -9 ~~ 10 ^ 9的坐标轴之中,有 10W个点(注意有些点可能在同一坐标上),然后有10W个询问,处理询问按照输入顺序处理,对于每个询问a,b    a == ...

  9. 使用WIX打包客户端程序

    原文:使用WIX打包客户端程序 用WPF为客户做了个小工具,打包的时候发现VS2012居然没有安装项目了,搜了下才知道现在推荐使用WIX来打包了http://wix.sourceforge.net/, ...

  10. hdu2050(递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2050 (1) n条直线最多分平面问题 题目大致如:n条直线,最多可以把平面分为多少个区域. 析:可能你 ...