[源码下载]

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

作者:webabcd

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

  • FileOpenPicker(文件选取窗口)
  • FolderPicker(文件夹选取窗口)
  • FileSavePicker(文件保存窗口)

示例
1、演示如何通过 FileOpenPicker 选择一个文件或多个文件
Picker/FileOpenPickerDemo.xaml

  1. <Page
  2. x:Class="Windows10.Picker.FileOpenPickerDemo"
  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. <Button Name="btnPickFile" Content="pick a file" Click="btnPickFile_Click" Margin="5" />
  16.  
  17. <Button Name="btnPickFiles" Content="pick multiple files" Click="btnPickFiles_Click" Margin="5" />
  18.  
  19. </StackPanel>
  20. </Grid>
  21. </Page>

Picker/FileOpenPickerDemo.xaml.cs

  1. /*
  2. * 演示如何通过 FileOpenPicker 选择一个文件或多个文件
  3. *
  4. * FileOpenPicker - 文件选择窗口
  5. * ViewMode - 文件选择窗口的视图模式,Windows.Storage.Pickers.PickerViewMode 枚举(List 或 Thumbnail)
  6. * SuggestedStartLocation - 文件选择窗口所显示的初始路径,Windows.Storage.Pickers.PickerLocationId 枚举
  7. * DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary, VideosLibrary, Objects3D, Unspecified
  8. * FileTypeFilter - 允许显示在文件选择窗口的文件类型集合(* 代表全部)
  9. * CommitButtonText - 文件选择窗口的提交按钮的显示文本,此按钮默认显示的文本为“打开”
  10. * PickSingleFileAsync() - 弹出文件选择窗口,以让用户选择一个文件
  11. * PickMultipleFilesAsync() - 弹出文件选择窗口,以让用户选择多个文件
  12. */
  13.  
  14. using System;
  15. using System.Collections.Generic;
  16. using Windows.Storage;
  17. using Windows.Storage.Pickers;
  18. using Windows.UI.Xaml;
  19. using Windows.UI.Xaml.Controls;
  20.  
  21. namespace Windows10.Picker
  22. {
  23. public sealed partial class FileOpenPickerDemo : Page
  24. {
  25. public FileOpenPickerDemo()
  26. {
  27. this.InitializeComponent();
  28. }
  29.  
  30. private async void btnPickFile_Click(object sender, RoutedEventArgs e)
  31. {
  32. // 选择一个文件
  33. FileOpenPicker openPicker = new FileOpenPicker();
  34. openPicker.CommitButtonText = "选中此文件";
  35. openPicker.ViewMode = PickerViewMode.Thumbnail;
  36. openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
  37. openPicker.FileTypeFilter.Add(".jpg");
  38. openPicker.FileTypeFilter.Add(".gif");
  39. openPicker.FileTypeFilter.Add(".png");
  40. // * 代表全部
  41. // openPicker.FileTypeFilter.Add("*");
  42.  
  43. // 弹出文件选择窗口
  44. StorageFile file = await openPicker.PickSingleFileAsync(); // 用户在“文件选择窗口”中完成操作后,会返回对应的 StorageFile 对象
  45. if (file != null)
  46. {
  47. lblMsg.Text = "选中文件: " + file.Name;
  48. }
  49. else
  50. {
  51. lblMsg.Text = "取消了";
  52. }
  53. }
  54.  
  55. private async void btnPickFiles_Click(object sender, RoutedEventArgs e)
  56. {
  57. // 选择多个文件
  58. FileOpenPicker openPicker = new FileOpenPicker();
  59. openPicker.ViewMode = PickerViewMode.List;
  60. openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
  61. openPicker.FileTypeFilter.Add("*");
  62.  
  63. // 弹出文件选择窗口
  64. IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync(); // 用户在“文件选择窗口”中完成操作后,会返回对应的 StorageFile 对象
  65. if (files.Count > )
  66. {
  67. lblMsg.Text = "选中文件: ";
  68. lblMsg.Text += Environment.NewLine;
  69. foreach (StorageFile file in files)
  70. {
  71. lblMsg.Text += (file.Name);
  72. lblMsg.Text += Environment.NewLine;
  73. }
  74. }
  75. else
  76. {
  77. lblMsg.Text = "取消了";
  78. }
  79. }
  80. }
  81. }

2、演示如何通过 FolderPicker 选择一个文件夹
Picker/FolderPickerDemo.xaml

  1. <Page
  2. x:Class="Windows10.Picker.FolderPickerDemo"
  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. <Button Name="btnPickFolder" Content="pick a folder" Click="btnPickFolder_Click" Margin="5" />
  16.  
  17. </StackPanel>
  18. </Grid>
  19. </Page>

Picker/FolderPickerDemo.xaml.cs

  1. /*
  2. * 演示如何通过 FolderPicker 选择一个文件夹
  3. *
  4. * FolderPicker - 文件夹选择窗口
  5. * ViewMode - 文件夹选择窗口的视图模式,Windows.Storage.Pickers.PickerViewMode 枚举(List 或 Thumbnail)
  6. * SuggestedStartLocation - 文件夹选择窗口所显示的初始路径,Windows.Storage.Pickers.PickerLocationId 枚举
  7. * DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary, VideosLibrary, Objects3D, Unspecified
  8. * FileTypeFilter - 允许显示在文件夹选择窗口的文件类型集合(只能显示符合要求的文件,但是无法选中)
  9. * CommitButtonText - 文件夹选择窗口的提交按钮的显示文本,此按钮默认显示的文本为“选择这个文件夹”
  10. * PickSingleFolderAsync() - 弹出文件夹选择窗口,以让用户选择一个文件夹
  11. */
  12.  
  13. using System;
  14. using Windows.Storage;
  15. using Windows.Storage.Pickers;
  16. using Windows.UI.Xaml;
  17. using Windows.UI.Xaml.Controls;
  18.  
  19. namespace Windows10.Picker
  20. {
  21. public sealed partial class FolderPickerDemo : Page
  22. {
  23. public FolderPickerDemo()
  24. {
  25. this.InitializeComponent();
  26. }
  27.  
  28. private async void btnPickFolder_Click(object sender, RoutedEventArgs e)
  29. {
  30. // 选择一个文件夹
  31. FolderPicker folderPicker = new FolderPicker();
  32. folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
  33. folderPicker.FileTypeFilter.Add(".docx");
  34. folderPicker.FileTypeFilter.Add(".xlsx");
  35. folderPicker.FileTypeFilter.Add(".pptx");
  36.  
  37. // 弹出文件夹选择窗口
  38. StorageFolder folder = await folderPicker.PickSingleFolderAsync(); // 用户在“文件夹选择窗口”中完成操作后,会返回对应的 StorageFolder 对象
  39. if (folder != null)
  40. {
  41. lblMsg.Text = "选中文件夹: " + folder.Name;
  42. }
  43. else
  44. {
  45. lblMsg.Text = "取消了";
  46. }
  47. }
  48. }
  49. }

3、演示如何通过 FileSavePicker 保存文件到指定路径
Picker/FileSavePickerDemo.xaml

  1. <Page
  2. x:Class="Windows10.Picker.FileSavePickerDemo"
  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. <Button Name="btnSaveFile" Content="save a file" Click="btnSaveFile_Click" Margin="5" />
  16.  
  17. </StackPanel>
  18. </Grid>
  19. </Page>

Picker/FileSavePickerDemo.xaml.cs

  1. /*
  2. * 演示如何通过 FileSavePicker 保存文件到指定路径
  3. *
  4. * FileSavePicker - 文件保存窗口
  5. * SuggestedStartLocation - 文件保存窗口所显示的初始路径,Windows.Storage.Pickers.PickerLocationId 枚举
  6. * DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary, VideosLibrary, Objects3D, Unspecified
  7. * SuggestedFileName - 需要保存的文件的默认文件名
  8. * SuggestedSaveFile - 需要保存的文件的默认 StorageFile 对象
  9. * FileTypeChoices - 可保存的扩展名集合(* 代表全部)
  10. * DefaultFileExtension - 默认扩展名
  11. * CommitButtonText - 文件保存窗口的提交按钮的显示文本,此按钮默认显示的文本为“保存”
  12. * PickSaveFileAsync() - 弹出文件保存窗口,以让用户保存文件
  13. */
  14.  
  15. using System;
  16. using System.Collections.Generic;
  17. using Windows.Storage;
  18. using Windows.Storage.Pickers;
  19. using Windows.Storage.Provider;
  20. using Windows.UI.Xaml;
  21. using Windows.UI.Xaml.Controls;
  22.  
  23. namespace Windows10.Picker
  24. {
  25. public sealed partial class FileSavePickerDemo : Page
  26. {
  27. public FileSavePickerDemo()
  28. {
  29. this.InitializeComponent();
  30. }
  31.  
  32. private async void btnSaveFile_Click(object sender, RoutedEventArgs e)
  33. {
  34. FileSavePicker savePicker = new FileSavePicker();
  35. savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
  36.  
  37. // 在扩展名选择框中将会显示:文本(.txt)
  38. savePicker.FileTypeChoices.Add("文本", new List<string>() { ".txt" });
  39. savePicker.SuggestedFileName = "webabcdFileSavePicker";
  40.  
  41. // 弹出文件保存窗口
  42. StorageFile file = await savePicker.PickSaveFileAsync(); // 用户在“文件保存窗口”中完成操作后,会返回对应的 StorageFile 对象
  43. if (file != null)
  44. {
  45. /*
  46. * 运行到此,只是在目标地址创建了一个没有任何内容的空白文件而已,接下来开始向文件写入内容
  47. */
  48.  
  49. // 告诉 Windows ,从此时开始要防止其它程序更新指定的文件
  50. CachedFileManager.DeferUpdates(file);
  51.  
  52. // 将指定的内容保存到指定的文件
  53. string textContent = "I am webabcd";
  54. await FileIO.WriteTextAsync(file, textContent);
  55.  
  56. // 告诉 Windows ,从此时开始允许其它程序更新指定的文件
  57. FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
  58. if (status == FileUpdateStatus.Complete)
  59. {
  60. lblMsg.Text = "文件 " + file.Name + " 保存成功";
  61. }
  62.  
  63. lblMsg.Text += Environment.NewLine;
  64. lblMsg.Text += "FileUpdateStatus: " + status.ToString();
  65. }
  66. else
  67. {
  68. lblMsg.Text = "取消了";
  69. }
  70. }
  71. }
  72. }

OK
[源码下载]

背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker的更多相关文章

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

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

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

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

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

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

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

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

  5. 背水一战 Windows 10 (100) - 应用间通信: 分享

    [源码下载] 背水一战 Windows 10 (100) - 应用间通信: 分享 作者:webabcd 介绍背水一战 Windows 10 之 应用间通信 分享 示例1.本例用于演示如何开发一个分享的 ...

  6. 背水一战 Windows 10 (27) - 控件(文本类): TextBlock

    [源码下载] 背水一战 Windows 10 (27) - 控件(文本类): TextBlock 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) TextBlock 示例 ...

  7. 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧

    [源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...

  8. 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue

    [源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...

  9. 背水一战 Windows 10 (9) - 资源: 资源限定符概述, 资源限定符示例

    [源码下载] 背水一战 Windows 10 (9) - 资源: 资源限定符概述, 资源限定符示例 作者:webabcd 介绍背水一战 Windows 10 之 资源 资源限定符概述 资源限定符示例 ...

随机推荐

  1. pythone函数基础(12)连接Redis,写数据,读数据,修改数据

    需要导入Resdis模块 import redisip = '127.0.0.1'password='123456'r = redis.Redis(host=ip,password=password, ...

  2. VBA实现表单自动编号

    效果:每次提交或刷新标段,表单案指定格式实现自动编号.如当天日期加数字编号的格式 即 2019年2月3日产生的第一张表单产生的编号格式为20190203-001 以此类推第二张表单编号为2019020 ...

  3. impala操作hase、hive

    impala中使用复杂类型(Hive):    如果Hive中创建的表带有复杂类型(array,struct,map),且储存格式(stored as textfile)为text或者默认,那么在im ...

  4. NodeManager介绍

    原文链接: http://blog.csdn.net/zhangzhebjut/article/details/37730013 参考文档: https://blog.csdn.net/u013384 ...

  5. laravel-更换语言包

    第一步:找语言包 找到比较靠谱的语言包(根据下载量与收藏量综合判断),而且要是laravel的 扩展的链接:https://packagist.org/packages/caouecs/laravel ...

  6. HTML元素的分类

    HTML元素的分类 EC前端 - HTML教程 块元素 div:无语义,常用于布局 aside:表示article元素的内容之外的与article元素的内容相关内容 figure:表示一段独立的流内容 ...

  7. java39

    String a= "hello.a.java;b.java;hello.java;hello.toha;"; //将每个分号的内容取出来 String[] res=a.split ...

  8. P2634 [国家集训队]聪聪可可

    淀粉质 第二道点分治的题 关于点分治的一点理解: 所谓点分治,其实就是把要求的问题(一般与路径有关)划分成两种情况 1.路径经过rt(根节点) 2.路径在根节点的子树内 我们只需要处理情况1,因为情况 ...

  9. 操作系统组成和工作原理以及cpu的工作原理

  10. 《Linux就该这么学》第十三天课程

    使用Apache服务部署静态网站 原创地址:https://www.linuxprobe.com/chapter-10.html 今天学了Apache,这只是RHCE课程的开始,估计后面越来越难 今天 ...