背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker
作者:webabcd
介绍
背水一战 Windows 10 之 选取器
- FileOpenPicker(文件选取窗口)
- FolderPicker(文件夹选取窗口)
- FileSavePicker(文件保存窗口)
示例
1、演示如何通过 FileOpenPicker 选择一个文件或多个文件
Picker/FileOpenPickerDemo.xaml
<Page
x:Class="Windows10.Picker.FileOpenPickerDemo"
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" /> <Button Name="btnPickFile" Content="pick a file" Click="btnPickFile_Click" Margin="5" /> <Button Name="btnPickFiles" Content="pick multiple files" Click="btnPickFiles_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
Picker/FileOpenPickerDemo.xaml.cs
/*
* 演示如何通过 FileOpenPicker 选择一个文件或多个文件
*
* FileOpenPicker - 文件选择窗口
* ViewMode - 文件选择窗口的视图模式,Windows.Storage.Pickers.PickerViewMode 枚举(List 或 Thumbnail)
* SuggestedStartLocation - 文件选择窗口所显示的初始路径,Windows.Storage.Pickers.PickerLocationId 枚举
* DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary, VideosLibrary, Objects3D, Unspecified
* FileTypeFilter - 允许显示在文件选择窗口的文件类型集合(* 代表全部)
* CommitButtonText - 文件选择窗口的提交按钮的显示文本,此按钮默认显示的文本为“打开”
* PickSingleFileAsync() - 弹出文件选择窗口,以让用户选择一个文件
* PickMultipleFilesAsync() - 弹出文件选择窗口,以让用户选择多个文件
*/ using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Picker
{
public sealed partial class FileOpenPickerDemo : Page
{
public FileOpenPickerDemo()
{
this.InitializeComponent();
} private async void btnPickFile_Click(object sender, RoutedEventArgs e)
{
// 选择一个文件
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.CommitButtonText = "选中此文件";
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".gif");
openPicker.FileTypeFilter.Add(".png");
// * 代表全部
// openPicker.FileTypeFilter.Add("*"); // 弹出文件选择窗口
StorageFile file = await openPicker.PickSingleFileAsync(); // 用户在“文件选择窗口”中完成操作后,会返回对应的 StorageFile 对象
if (file != null)
{
lblMsg.Text = "选中文件: " + file.Name;
}
else
{
lblMsg.Text = "取消了";
}
} private async void btnPickFiles_Click(object sender, RoutedEventArgs e)
{
// 选择多个文件
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
openPicker.FileTypeFilter.Add("*"); // 弹出文件选择窗口
IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync(); // 用户在“文件选择窗口”中完成操作后,会返回对应的 StorageFile 对象
if (files.Count > )
{
lblMsg.Text = "选中文件: ";
lblMsg.Text += Environment.NewLine;
foreach (StorageFile file in files)
{
lblMsg.Text += (file.Name);
lblMsg.Text += Environment.NewLine;
}
}
else
{
lblMsg.Text = "取消了";
}
}
}
}
2、演示如何通过 FolderPicker 选择一个文件夹
Picker/FolderPickerDemo.xaml
<Page
x:Class="Windows10.Picker.FolderPickerDemo"
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" /> <Button Name="btnPickFolder" Content="pick a folder" Click="btnPickFolder_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
Picker/FolderPickerDemo.xaml.cs
/*
* 演示如何通过 FolderPicker 选择一个文件夹
*
* FolderPicker - 文件夹选择窗口
* ViewMode - 文件夹选择窗口的视图模式,Windows.Storage.Pickers.PickerViewMode 枚举(List 或 Thumbnail)
* SuggestedStartLocation - 文件夹选择窗口所显示的初始路径,Windows.Storage.Pickers.PickerLocationId 枚举
* DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary, VideosLibrary, Objects3D, Unspecified
* FileTypeFilter - 允许显示在文件夹选择窗口的文件类型集合(只能显示符合要求的文件,但是无法选中)
* CommitButtonText - 文件夹选择窗口的提交按钮的显示文本,此按钮默认显示的文本为“选择这个文件夹”
* PickSingleFolderAsync() - 弹出文件夹选择窗口,以让用户选择一个文件夹
*/ using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Picker
{
public sealed partial class FolderPickerDemo : Page
{
public FolderPickerDemo()
{
this.InitializeComponent();
} private async void btnPickFolder_Click(object sender, RoutedEventArgs e)
{
// 选择一个文件夹
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add(".docx");
folderPicker.FileTypeFilter.Add(".xlsx");
folderPicker.FileTypeFilter.Add(".pptx"); // 弹出文件夹选择窗口
StorageFolder folder = await folderPicker.PickSingleFolderAsync(); // 用户在“文件夹选择窗口”中完成操作后,会返回对应的 StorageFolder 对象
if (folder != null)
{
lblMsg.Text = "选中文件夹: " + folder.Name;
}
else
{
lblMsg.Text = "取消了";
}
}
}
}
3、演示如何通过 FileSavePicker 保存文件到指定路径
Picker/FileSavePickerDemo.xaml
<Page
x:Class="Windows10.Picker.FileSavePickerDemo"
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" /> <Button Name="btnSaveFile" Content="save a file" Click="btnSaveFile_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
Picker/FileSavePickerDemo.xaml.cs
/*
* 演示如何通过 FileSavePicker 保存文件到指定路径
*
* FileSavePicker - 文件保存窗口
* SuggestedStartLocation - 文件保存窗口所显示的初始路径,Windows.Storage.Pickers.PickerLocationId 枚举
* DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary, VideosLibrary, Objects3D, Unspecified
* SuggestedFileName - 需要保存的文件的默认文件名
* SuggestedSaveFile - 需要保存的文件的默认 StorageFile 对象
* FileTypeChoices - 可保存的扩展名集合(* 代表全部)
* DefaultFileExtension - 默认扩展名
* CommitButtonText - 文件保存窗口的提交按钮的显示文本,此按钮默认显示的文本为“保存”
* PickSaveFileAsync() - 弹出文件保存窗口,以让用户保存文件
*/ using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Picker
{
public sealed partial class FileSavePickerDemo : Page
{
public FileSavePickerDemo()
{
this.InitializeComponent();
} private async void btnSaveFile_Click(object sender, RoutedEventArgs e)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; // 在扩展名选择框中将会显示:文本(.txt)
savePicker.FileTypeChoices.Add("文本", new List<string>() { ".txt" });
savePicker.SuggestedFileName = "webabcdFileSavePicker"; // 弹出文件保存窗口
StorageFile file = await savePicker.PickSaveFileAsync(); // 用户在“文件保存窗口”中完成操作后,会返回对应的 StorageFile 对象
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 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker的更多相关文章
- 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater
[源码下载] 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater 作者:webabcd 介绍背水一战 Windows 10 之 选取器 CachedFileUp ...
- 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器
[源码下载] 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件保存选取器 示例1.演示如何 ...
- 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器
[源码下载] 背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件打开选取器 示例1.演示如何 ...
- 背水一战 Windows 10 (96) - 选取器: ContactPicker
[源码下载] 背水一战 Windows 10 (96) - 选取器: ContactPicker 作者:webabcd 介绍背水一战 Windows 10 之 选取器 ContactPicker(联系 ...
- 背水一战 Windows 10 (100) - 应用间通信: 分享
[源码下载] 背水一战 Windows 10 (100) - 应用间通信: 分享 作者:webabcd 介绍背水一战 Windows 10 之 应用间通信 分享 示例1.本例用于演示如何开发一个分享的 ...
- 背水一战 Windows 10 (27) - 控件(文本类): TextBlock
[源码下载] 背水一战 Windows 10 (27) - 控件(文本类): TextBlock 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) TextBlock 示例 ...
- 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧
[源码下载] 背水一战 Windows 10 (21) - 绑定: x:Bind 绑定, x:Bind 绑定之 x:Phase, 使用绑定过程中的一些技巧 作者:webabcd 介绍背水一战 Wind ...
- 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue
[源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...
- 背水一战 Windows 10 (9) - 资源: 资源限定符概述, 资源限定符示例
[源码下载] 背水一战 Windows 10 (9) - 资源: 资源限定符概述, 资源限定符示例 作者:webabcd 介绍背水一战 Windows 10 之 资源 资源限定符概述 资源限定符示例 ...
随机推荐
- Tomcat start.bat闪退:JRE_HOME环境变量配置不对
最近在配置Tomcat上遇到startup.bat启动闪退,在网上找了很多方法,都没解决.后来在网上找到两种问题,更改了两次,解决了.现将我遇到的问题分享,希望对遇到同样问题有帮助. 1.很多初学者对 ...
- Tomcat7 目录详解
1.bin:该目录下存放的是二进制可执行文件,如果是安装版,那么这个目录下会有两个exe文件:tomcat6.exe.tomcat6w.exe,前者是在控制台下启动Tomcat,后者是弹出UGI窗口启 ...
- c#Dapper mysql按时间段查询和过滤
#endregion /// <summary> /// 根据条件获取集合 /// </summary> /// <param name="id"&g ...
- 对js原型及构造函数的相关理解
一.js中的原型创建(声明)一个函数,浏览器在内存中会创建一个对象.每个函数都默认会有一个属性prototype指向了这个对象,就是说prototype的属性的值就是这个对象.此对象就是该函数的原型对 ...
- springCloud笔记
分布式和集群的理解:比如在一个厨房有两个厨师,一个炒菜,一个洗菜,各自做不同的事情,但是却在合作,这种叫做分布式,两个都在炒菜或者都在做菜,就叫做集群. eureka的是springCloud的注册中 ...
- PHP 使用数字作为SESSION的Key,一刷新页面session丢失,Session消失,无法存储
PHP 使用数字作为SESSION的Key,一刷新页面session丢失,Session消失,无法存储 项目中有用到md5截取做session key值的,有些md5截取出来的部分是纯数字的,导致部分 ...
- java39
String a= "hello.a.java;b.java;hello.java;hello.toha;"; //将每个分号的内容取出来 String[] res=a.split ...
- JAVA获取微信小程序openid和获取公众号openid,以及通过openid获取用户信息
一,首先说明下这个微信的openid 为了识别用户,每个用户针对每个公众号会产生一个安全的OpenID,如果需要在多公众号.移动应用之间做用户共通,则需前往微信开放平台,将这些公众号和应用绑定到一个开 ...
- k8s初始化搭建方法
http://www.cnblogs.com/cocowool/p/kubeadm_install_kubernetes.html https://www.kubernetes.org.cn/doc- ...
- 学习Acegi应用到实际项目中(6)
在企业应用中,用户的用户名.密码和角色等信息一般存放在RDBMS(关系数据库)中.前面几节我们采用的是InMemoryDaoImpl,即基于内存的存放方式.这节我们将采用RDBMS存储用户信息. Us ...