背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器
作者:webabcd
介绍
背水一战 Windows 10 之 选取器
- 自定义文件保存选取器
示例
1、演示如何开发自定义文件保存选取器
App.xaml.cs
// 通过文件保存选取器激活应用程序时所调用的方法
protected override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args)
{
var rootFrame = new Frame();
rootFrame.Navigate(typeof(Windows10.Picker.MySavePicker), args);
Window.Current.Content = rootFrame; Window.Current.Activate();
}
Picker/MySavePicker.xaml
<Page
x:Class="Windows10.Picker.MySavePicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.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="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> </StackPanel>
</Grid>
</Page>
Picker/MySavePicker.xaml.cs
/*
* 演示如何开发自定义文件保存选取器
*
* 1、在 Package.appxmanifest 中新增一个“文件保存选取器”声明,并做相关配置
* 2、在 App.xaml.cs 中 override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args),如果 app 是由文件保存选取器激活的,则会调用此方法
*
* FileSavePickerActivatedEventArgs - 通过“文件保存选取器”激活应用程序时的事件参数
* FileSavePickerUI - 获取 FileSavePickerUI 对象
* Kind - 此 app 被激活的类型(ActivationKind 枚举)
* 比如,如果是通过“文件打开选取器”激活的话,则此值为 FileOpenPicker
* PreviousExecutionState - 此 app 被激活前的状态(ApplicationExecutionState 枚举)
* 比如,如果此 app 被激活前就是运行状态的或,则此值为 Running
* SplashScreen - 获取此 app 的 SplashScreen 对象
* CallerPackageFamilyName - 获取激活了此 app 的应用的包名(但是实际测试发现,获取到的却是此 app 的包名)
* User - 获取激活了此 app 的 User 对象
*
* FileSavePickerUI - 自定义文件保存选取器的帮助类
* AllowedFileTypes - 允许的文件类型,只读
* Title - 将在“自定义文件保存选取器”上显示的标题
* FileName - 需要保存的文件名(包括文件名和扩展名),只读
* TrySetFileName(string value) - 尝试指定需要保存的文件名(包括文件名和扩展名)
* FileNameChanged - 用户在文件名文本框中更改文件名或在文件类型下拉框中更改扩展名时触发的事件
* TargetFileRequested - 用户提交保存时触发的事件(事件参数:TargetFileRequestedEventArgs)
*
* TargetFileRequestedEventArgs
* Request - 返回 TargetFileRequest 对象
*
* TargetFileRequest
* TargetFile - 目标文件对象,用于返回给 client
* GetDeferral() - 获取异步操作对象,同时开始异步操作,之后通过 Complete() 通知完成异步操作
*/ using System;
using System.Collections.Generic;
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 Windows10.Picker
{
public sealed partial class MySavePicker : Page
{
private FileSavePickerUI _fileSavePickerUI; public MySavePicker()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 获取 FileSavePickerUI 对象(从 App.xaml.cs 传来的)
FileSavePickerActivatedEventArgs args = (FileSavePickerActivatedEventArgs)e.Parameter;
_fileSavePickerUI = args.FileSavePickerUI; _fileSavePickerUI.Title = "自定义文件保存选取器"; // 通过 AllowedFileTypes 获取到的允许的扩展名是在调用端的 FileSavePicker.FileTypeChoices 中配置的,实际保存扩展名可以不与其匹配
IReadOnlyList<string> allowedFileTypes = _fileSavePickerUI.AllowedFileTypes;
lblMsg.Text = "allowedFileTypes: " + string.Join(",", allowedFileTypes);
lblMsg.Text += Environment.NewLine; lblMsg.Text += "Kind: " + args.Kind;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "SplashScreen.ImageLocation: " + args.SplashScreen.ImageLocation;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "PreviousExecutionState: " + args.PreviousExecutionState;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "CallerPackageFamilyName: " + args.CallerPackageFamilyName;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "User.NonRoamableId: " + args.User.NonRoamableId;
lblMsg.Text += Environment.NewLine; _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;
});
}
}
}
2、演示如何调用自定义文件保存选取器
Picker/MySavePickerDemo.xaml
<Page
x:Class="Windows10.Picker.MySavePickerDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.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="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5">
<Run>
如果需要激活自定义的文件保存窗口,请在弹出的选取器窗口的左侧的导航列表中选择相应的 app
</Run>
</TextBlock> <Button Name="btnMySavePicker" Content="弹出文件保存窗口" Click="btnMySavePicker_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
Picker/MySavePickerDemo.xaml.cs
/*
* 演示如何调用自定义文件保存选取器
*
* 自定义文件保存选取器参见 MySavePicker.xaml
*/ using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.UI.Xaml.Controls; namespace Windows10.Picker
{
public sealed partial class MySavePickerDemo : Page
{
public MySavePickerDemo()
{
this.InitializeComponent();
} private async void btnMySavePicker_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.FileTypeChoices.Add("文本", new List<string>() { ".txt" }); // 弹出文件保存窗口
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
/*
* 运行到此,只是在目标地址创建了一个没有任何内容的空白文件而已,接下来开始向文件写入内容
*/ // 告诉 Windows ,从此时开始要防止其它程序更新指定的文件
CachedFileManager.DeferUpdates(file); // 将指定的内容保存到指定的文件
string textContent = "I am webabcd";
await FileIO.WriteTextAsync(file, textContent); // 告诉 Windows ,从此时开始允许其它程序更新指定的文件
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status == FileUpdateStatus.Complete)
{
lblMsg.Text = "文件 " + file.Name + " 保存成功";
} lblMsg.Text += Environment.NewLine;
lblMsg.Text += "FileUpdateStatus: " + status.ToString();
}
else
{
lblMsg.Text = "取消了";
}
}
}
}
OK
[源码下载]
背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器的更多相关文章
- 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器
[源码下载] 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件打开选取器 示例1.演示如何 ...
- 重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口
原文:重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (26) ...
- 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater
[源码下载] 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater 作者:webabcd 介绍背水一战 Windows 10 之 选取器 CachedFileUp ...
- 背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker
[源码下载] 背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker 作者:webabcd 介绍背水一战 Wi ...
- 背水一战 Windows 10 (96) - 选取器: ContactPicker
[源码下载] 背水一战 Windows 10 (96) - 选取器: ContactPicker 作者:webabcd 介绍背水一战 Windows 10 之 选取器 ContactPicker(联系 ...
- 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件
[源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...
- 背水一战 Windows 10 (99) - 关联启动: 关联指定的文件类型, 关联指定的协议
[源码下载] 背水一战 Windows 10 (99) - 关联启动: 关联指定的文件类型, 关联指定的协议 作者:webabcd 介绍背水一战 Windows 10 之 关联启动 关联指定的文件类型 ...
- 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换
[源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...
- 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri
[源码下载] 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri 作者:webabcd 介绍背水一战 Windows 10 之 关联启动 ...
随机推荐
- jmeter javamail 邮件格式再优化(由详情——>改为统计)
前言:之前扩展的ant—jmeter支持邮件附件形式上传以及邮件内容的html文件格式. 如图: 由于邮件的内容格式是详情信息,也就是说直观的显示的是case,但由于case的增加,邮件内容越来越大! ...
- anaconda安装qt错误
windows下miniconda2安装python3下的qt包报错 conda install qt ERROR conda.core.link:_execute_actions(330): An ...
- Chapter_4_JAVA作业
一.类的封装,继承与多态 1.课前预习 1.1 举列现实生活中的封装,以及简述在程序中什么是属性的封装? 1.1.1 将东西捆绑在一起,如集成芯片:高压电线等等 1.1.2 封装就是将属性私有化,提供 ...
- 使用DOS命令无法启动MySQL
今天使用命令dos 命令 net start mysql 启动mysql的使用出现以下情况 无法正常启动mysql服务. 原因是: 启动dos命令窗口时的用户权限太低,无法正常使用 解决办法: 搜索c ...
- 用SAX解析xml文件,java
(此文为(https://www.imooc.com/video/4482)之随笔) 1.用SAX解析xml文件大致分为三步 写了一个XML文件作为例子 (1)main方法代码如下: import j ...
- linux学习第十六天 (Linux就该这么学)
今生讲了邮件的产生和解决和实际问题,把前两天的和这节邮箱系统统一布置,又统一复习和学习了一下,
- 博客1--tensorflow的图像基本处理操作
话不多,具体内容在开源中国里我的博客:https://my.oschina.net/u/3770644 代码: #!/usr/bin/env python# -*- coding:utf-8 -*-# ...
- 《修炼之道:.NET开发要点精讲》读书笔记(一)
CLR 公共语言运行库 没有CLR的存在,就不能讲该中间件转换成对应操作系统中的机器指令. 程序集是非完全编译的产物,它兼备了源代码和本地代码的特性,是一种介于源代码和本地代码之间的独立存在的一种数据 ...
- HttpWebRequest.AddRange 支持long类型
很久很久以前,在哪个FAT32格式还流行的年代,文件大小普遍还没超过4G的年代,.Net已经出来了. 而那时候.Net实现的HTTP断点续传协议,还没预料到如此普及(我猜的).那时候的HttpWebR ...
- ssh 免密登录阿里云主机
在网上找了好几篇教程,都不好使. 终于在这篇找到了答案 解决方案: 在 sshd_config 里面将这一项: AuthorizedKeysFile .ssh/authorized_keys 被我修改 ...