[源码下载]

背水一战 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) - 选取器: 自定义文件保存选取器的更多相关文章

  1. 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器

    [源码下载] 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件打开选取器 示例1.演示如何 ...

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

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

  3. 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater

    [源码下载] 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater 作者:webabcd 介绍背水一战 Windows 10 之 选取器 CachedFileUp ...

  4. 背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker

    [源码下载] 背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker 作者:webabcd 介绍背水一战 Wi ...

  5. 背水一战 Windows 10 (96) - 选取器: ContactPicker

    [源码下载] 背水一战 Windows 10 (96) - 选取器: ContactPicker 作者:webabcd 介绍背水一战 Windows 10 之 选取器 ContactPicker(联系 ...

  6. 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    [源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...

  7. 背水一战 Windows 10 (99) - 关联启动: 关联指定的文件类型, 关联指定的协议

    [源码下载] 背水一战 Windows 10 (99) - 关联启动: 关联指定的文件类型, 关联指定的协议 作者:webabcd 介绍背水一战 Windows 10 之 关联启动 关联指定的文件类型 ...

  8. 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换

    [源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...

  9. 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri

    [源码下载] 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri 作者:webabcd 介绍背水一战 Windows 10 之 关联启动 ...

随机推荐

  1. [leetcode]432. All O`one Data Structure全O(1)数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  2. nio实现原理

    nio是事件驱动,当soket有消息过来时才开启线程,bio每当有连接时,就开启一个线程,长连接的话,就有太多的空闲连接占用线程内存 nio是非阻塞长连接 ServerSocketChannel:饭店 ...

  3. idea配置git,查看git代码&拉取git项目至本地

    1.点击file,右键选择setting 选择本地git安装路径 Ps:从git上导入一个全新的maven项目 点击clone按钮后,会弹出如下截图弹窗,点击 NO 项目已经拉取到本地,然后点击ope ...

  4. Linux 学习笔记 3:Shell 基础

    1.Shell的工作原理 Shell是用户和操作系统之间的接口.当用户登录系统,有一个Shell进程随之启动,并在用户注销时终止.用户输入的命令是Shell输入. 处理命令的步骤:查找命令中的元字符: ...

  5. devexpress总结 accordionControl 加载panelcontrol 的快捷方式

    先说保存: UserControl control; private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars. ...

  6. hbase-运维命令

    hbase 问题 不一致问题 meta表不一致问题 hdfs文件不一致问题 hbase hbck hbase hbck 用于检测和修改hbase底层文件问题.检测像master,region serv ...

  7. HTTP知识点【总结篇】

    1.什么是HTTP协议? 客户端和服务器之间数据传输的格式规范.全拼:HyperText Transfer Protocol:超文本传输协议. 2.http协议是无状态协议?怎么解决无状态协议? 无状 ...

  8. Spring Boot 异常处理

    Spring Boot 异常处理 本节介绍一下 Spring Boot 启动时是如何处理异常的?核心类是 SpringBootExceptionReporter 和 SpringBootExcepti ...

  9. 返回上一页 html A标记代码

    <a class="sjad" href="#" onClick="javascript:history.back(-1);"> ...

  10. java -相关技术

    地址:      http://www.codeyyy.com/java/11-35-52.html