[源码下载]

背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器

作者:webabcd

介绍
背水一战 Windows 10 之 选取器

  • 自定义文件保存选取器

示例
1、演示如何开发自定义文件保存选取器
App.xaml.cs

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

Picker/MySavePicker.xaml

  1. <Page
  2. x:Class="Windows10.Picker.MySavePicker"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Picker"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <TextBlock Name="lblMsg" Margin="5" />
  14.  
  15. </StackPanel>
  16. </Grid>
  17. </Page>

Picker/MySavePicker.xaml.cs

  1. /*
  2. * 演示如何开发自定义文件保存选取器
  3. *
  4. * 1、在 Package.appxmanifest 中新增一个“文件保存选取器”声明,并做相关配置
  5. * 2、在 App.xaml.cs 中 override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args),如果 app 是由文件保存选取器激活的,则会调用此方法
  6. *
  7. * FileSavePickerActivatedEventArgs - 通过“文件保存选取器”激活应用程序时的事件参数
  8. * FileSavePickerUI - 获取 FileSavePickerUI 对象
  9. * Kind - 此 app 被激活的类型(ActivationKind 枚举)
  10. * 比如,如果是通过“文件打开选取器”激活的话,则此值为 FileOpenPicker
  11. * PreviousExecutionState - 此 app 被激活前的状态(ApplicationExecutionState 枚举)
  12. * 比如,如果此 app 被激活前就是运行状态的或,则此值为 Running
  13. * SplashScreen - 获取此 app 的 SplashScreen 对象
  14. * CallerPackageFamilyName - 获取激活了此 app 的应用的包名(但是实际测试发现,获取到的却是此 app 的包名)
  15. * User - 获取激活了此 app 的 User 对象
  16. *
  17. * FileSavePickerUI - 自定义文件保存选取器的帮助类
  18. * AllowedFileTypes - 允许的文件类型,只读
  19. * Title - 将在“自定义文件保存选取器”上显示的标题
  20. * FileName - 需要保存的文件名(包括文件名和扩展名),只读
  21. * TrySetFileName(string value) - 尝试指定需要保存的文件名(包括文件名和扩展名)
  22. * FileNameChanged - 用户在文件名文本框中更改文件名或在文件类型下拉框中更改扩展名时触发的事件
  23. * TargetFileRequested - 用户提交保存时触发的事件(事件参数:TargetFileRequestedEventArgs)
  24. *
  25. * TargetFileRequestedEventArgs
  26. * Request - 返回 TargetFileRequest 对象
  27. *
  28. * TargetFileRequest
  29. * TargetFile - 目标文件对象,用于返回给 client
  30. * GetDeferral() - 获取异步操作对象,同时开始异步操作,之后通过 Complete() 通知完成异步操作
  31. */
  32.  
  33. using System;
  34. using System.Collections.Generic;
  35. using Windows.ApplicationModel.Activation;
  36. using Windows.Storage;
  37. using Windows.Storage.Pickers.Provider;
  38. using Windows.UI.Core;
  39. using Windows.UI.Xaml.Controls;
  40. using Windows.UI.Xaml.Navigation;
  41.  
  42. namespace Windows10.Picker
  43. {
  44. public sealed partial class MySavePicker : Page
  45. {
  46. private FileSavePickerUI _fileSavePickerUI;
  47.  
  48. public MySavePicker()
  49. {
  50. this.InitializeComponent();
  51. }
  52.  
  53. protected override void OnNavigatedTo(NavigationEventArgs e)
  54. {
  55. // 获取 FileSavePickerUI 对象(从 App.xaml.cs 传来的)
  56. FileSavePickerActivatedEventArgs args = (FileSavePickerActivatedEventArgs)e.Parameter;
  57. _fileSavePickerUI = args.FileSavePickerUI;
  58.  
  59. _fileSavePickerUI.Title = "自定义文件保存选取器";
  60.  
  61. // 通过 AllowedFileTypes 获取到的允许的扩展名是在调用端的 FileSavePicker.FileTypeChoices 中配置的,实际保存扩展名可以不与其匹配
  62. IReadOnlyList<string> allowedFileTypes = _fileSavePickerUI.AllowedFileTypes;
  63. lblMsg.Text = "allowedFileTypes: " + string.Join(",", allowedFileTypes);
  64. lblMsg.Text += Environment.NewLine;
  65.  
  66. lblMsg.Text += "Kind: " + args.Kind;
  67. lblMsg.Text += Environment.NewLine;
  68. lblMsg.Text += "SplashScreen.ImageLocation: " + args.SplashScreen.ImageLocation;
  69. lblMsg.Text += Environment.NewLine;
  70. lblMsg.Text += "PreviousExecutionState: " + args.PreviousExecutionState;
  71. lblMsg.Text += Environment.NewLine;
  72. lblMsg.Text += "CallerPackageFamilyName: " + args.CallerPackageFamilyName;
  73. lblMsg.Text += Environment.NewLine;
  74. lblMsg.Text += "User.NonRoamableId: " + args.User.NonRoamableId;
  75. lblMsg.Text += Environment.NewLine;
  76.  
  77. _fileSavePickerUI.TargetFileRequested += _fileSavePickerUI_TargetFileRequested;
  78. }
  79.  
  80. protected override void OnNavigatedFrom(NavigationEventArgs e)
  81. {
  82. _fileSavePickerUI.TargetFileRequested -= _fileSavePickerUI_TargetFileRequested;
  83. }
  84.  
  85. private async void _fileSavePickerUI_TargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs args)
  86. {
  87. // 异步操作
  88. var deferral = args.Request.GetDeferral();
  89.  
  90. try
  91. {
  92. // 在指定的地址新建一个没有任何内容的空白文件
  93. StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(sender.FileName, CreationCollisionOption.GenerateUniqueName);
  94.  
  95. // 设置 TargetFile,“自定义文件保存选取器”的调用端会收到此对象
  96. args.Request.TargetFile = file;
  97. }
  98. catch (Exception ex)
  99. {
  100. // 输出异常信息
  101. OutputMessage(ex.ToString());
  102. }
  103. finally
  104. {
  105. // 完成异步操作
  106. deferral.Complete();
  107. }
  108. }
  109.  
  110. private async void OutputMessage(string msg)
  111. {
  112. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  113. {
  114. lblMsg.Text = msg;
  115. });
  116. }
  117. }
  118. }

2、演示如何调用自定义文件保存选取器
Picker/MySavePickerDemo.xaml

  1. <Page
  2. x:Class="Windows10.Picker.MySavePickerDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Picker"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <TextBlock Name="lblMsg" Margin="5">
  14. <Run>
  15. 如果需要激活自定义的文件保存窗口,请在弹出的选取器窗口的左侧的导航列表中选择相应的 app
  16. </Run>
  17. </TextBlock>
  18.  
  19. <Button Name="btnMySavePicker" Content="弹出文件保存窗口" Click="btnMySavePicker_Click" Margin="5" />
  20.  
  21. </StackPanel>
  22. </Grid>
  23. </Page>

Picker/MySavePickerDemo.xaml.cs

  1. /*
  2. * 演示如何调用自定义文件保存选取器
  3. *
  4. * 自定义文件保存选取器参见 MySavePicker.xaml
  5. */
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using Windows.Storage;
  10. using Windows.Storage.Pickers;
  11. using Windows.Storage.Provider;
  12. using Windows.UI.Xaml.Controls;
  13.  
  14. namespace Windows10.Picker
  15. {
  16. public sealed partial class MySavePickerDemo : Page
  17. {
  18. public MySavePickerDemo()
  19. {
  20. this.InitializeComponent();
  21. }
  22.  
  23. private async void btnMySavePicker_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
  24. {
  25. FileSavePicker savePicker = new FileSavePicker();
  26. savePicker.FileTypeChoices.Add("文本", new List<string>() { ".txt" });
  27.  
  28. // 弹出文件保存窗口
  29. StorageFile file = await savePicker.PickSaveFileAsync();
  30. if (file != null)
  31. {
  32. /*
  33. * 运行到此,只是在目标地址创建了一个没有任何内容的空白文件而已,接下来开始向文件写入内容
  34. */
  35.  
  36. // 告诉 Windows ,从此时开始要防止其它程序更新指定的文件
  37. CachedFileManager.DeferUpdates(file);
  38.  
  39. // 将指定的内容保存到指定的文件
  40. string textContent = "I am webabcd";
  41. await FileIO.WriteTextAsync(file, textContent);
  42.  
  43. // 告诉 Windows ,从此时开始允许其它程序更新指定的文件
  44. FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
  45. if (status == FileUpdateStatus.Complete)
  46. {
  47. lblMsg.Text = "文件 " + file.Name + " 保存成功";
  48. }
  49.  
  50. lblMsg.Text += Environment.NewLine;
  51. lblMsg.Text += "FileUpdateStatus: " + status.ToString();
  52. }
  53. else
  54. {
  55. lblMsg.Text = "取消了";
  56. }
  57. }
  58. }
  59. }

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. BootStrap插件

    站点引用 Bootstrap 插件的方式有两种: 单独引用:使用 Bootstrap 的个别的 *.js 文件.一些插件和 CSS 组件依赖于其他插件.如果您单独引用插件,请先确保弄清这些插件之间的依 ...

  2. 自动化测试框架对比(UIAutomator、Appium)

    在Android端,appium基于WebDriver协议,利用Bootstrap.jar,最后通过调⽤用UiAutomator的命令,实现App的自动化测试. UiAutomator测试框架是And ...

  3. jquery点击回到顶部

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. express基础项目创建

    https://www.cnblogs.com/zhentaoo/p/6392248.html

  5. JavaScript并发模型与Event Loop (转载)

    并发模型可视化描述 model.svg 如上图所示,Javascript执行引擎的主线程运行的时候,产生堆(heap)和栈(stack),程序中代码依次进入栈中等待执行, 若执行时遇到异步方法,该异步 ...

  6. skynet记录7:第一个服务logger和第二个服务bootstrap

    (1)logger是skynet_context_new创建:skynet_context及mq,模块create和init (2)bootstrap启动过程:snlua时一个lua的so,对应的sn ...

  7. HDU 6041.I Curse Myself 无向仙人掌图

    I Curse Myself Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  8. npm安装cnpm、vue、react

    安装cnpmnpm install -g cnpm --registry=https://registry.npm.taobao.org 安装vuecnpm install --global vue- ...

  9. tomcat与iis公用80端口(已经发布.net项目现在开发Java项目时tomcat在eclipse中localhost:8080打不开问题)

    在开发过.net项目的电脑上安装eclipse配置tomcat运行时打不开页面问题描述,这也是本人亲生经历,找了好多资料网上大多都是tomcat配置问题描述,今天突然想到是不是IIS的问题,果然上网一 ...

  10. Swagger2基本注解使用

    @Api:用在请求的类上,表示对类的说明 tags="说明该类的作用,可以在UI界面上看到的注解" value="该参数没什么意义,在UI界面上也看到,所以不需要配置&q ...