原文:重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口

[源码下载]

重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 选取器

  • FileOpenPicker - 选择一个文件或多个文件
  • FolderPicker - 选择一个文件夹
  • FileSavePicker - 保存文件到指定路径

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

<Page
x:Class="XamlDemo.Picker.FileOpenPickerDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.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="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnPickFile" Content="pick a file" Click="btnPickFile_Click_1" Margin="0 10 0 0" /> <Button Name="btnPickFiles" Content="pick multiple files" Click="btnPickFiles_Click_1" Margin="0 10 0 0" /> </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
* FileTypeFilter - 允许显示在文件选择窗口的文件类型集合
* CommitButtonText - 文件选择窗口的提交按钮的显示文本,此按钮默认显示的文本为“打开”
* PickSingleFileAsync() - 弹出文件选择窗口,以让用户选择一个文件
* PickMultipleFilesAsync() - 弹出文件选择窗口,以让用户选择多个文件
*/ using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Picker
{
public sealed partial class FileOpenPickerDemo : Page
{
public FileOpenPickerDemo()
{
this.InitializeComponent();
} private async void btnPickFile_Click_1(object sender, RoutedEventArgs e)
{
if (XamlDemo.Common.Helper.EnsureUnsnapped())
{
// 选择一个文件
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.CommitButtonText = "选中此文件";
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".gif");
openPicker.FileTypeFilter.Add(".png"); // 弹出文件选择窗口
StorageFile file = await openPicker.PickSingleFileAsync(); // 用户在“文件选择窗口”中完成操作后,会返回对应的 StorageFile 对象
if (file != null)
{
lblMsg.Text = "选中文件: " + file.Name;
}
else
{
lblMsg.Text = "取消了";
}
}
} private async void btnPickFiles_Click_1(object sender, RoutedEventArgs e)
{
if (XamlDemo.Common.Helper.EnsureUnsnapped())
{
// 选择多个文件
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="XamlDemo.Picker.FolderPickerDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.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="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnPickFolder" Content="pick a folder" Click="btnPickFolder_Click_1" Margin="0 10 0 0 " /> </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
* FileTypeFilter - 允许显示在文件夹选择窗口的文件类型集合(只能显示符合要求的文件,但是无法选中)
* CommitButtonText - 文件夹选择窗口的提交按钮的显示文本,此按钮默认显示的文本为“选择这个文件夹”
* PickSingleFolderAsync() - 弹出文件夹选择窗口,以让用户选择一个文件夹
*/ using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Picker
{
public sealed partial class FolderPickerDemo : Page
{
public FolderPickerDemo()
{
this.InitializeComponent();
} private async void btnPickFolder_Click_1(object sender, RoutedEventArgs e)
{
if (XamlDemo.Common.Helper.EnsureUnsnapped())
{
// 选择一个文件夹
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="XamlDemo.Picker.FileSavePickerDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Pikcer"
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="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnSaveFile" Content="save a file" Click="btnSaveFile_Click_1" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>

Picker/FileSavePickerDemo.xaml.cs

/*
* 演示如何通过 FileSavePicker 保存文件到指定路径
*
* FileSavePicker - 文件保存窗口
* SuggestedStartLocation - 文件保存窗口所显示的初始路径,Windows.Storage.Pickers.PickerLocationId 枚举
* DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary,VideosLibrary
* 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 XamlDemo.Picker
{
public sealed partial class FileSavePickerDemo : Page
{
public FileSavePickerDemo()
{
this.InitializeComponent();
} private async void btnSaveFile_Click_1(object sender, RoutedEventArgs e)
{
if (XamlDemo.Common.Helper.EnsureUnsnapped())
{
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 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口的更多相关文章

  1. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  2. 重新想象 Windows 8 Store Apps (28) - 选取器: CachedFileUpdater(缓存文件更新程序)

    原文:重新想象 Windows 8 Store Apps (28) - 选取器: CachedFileUpdater(缓存文件更新程序) [源码下载] 重新想象 Windows 8 Store App ...

  3. 重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口

    原文:重新想象 Windows 8 Store Apps (27) - 选取器: 联系人选取窗口, 自定义联系人选取窗口 [源码下载] 重新想象 Windows 8 Store Apps (27) - ...

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

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

  5. 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo

    [源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...

  6. 重新想象 Windows 8 Store Apps (41) - 打印

    [源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...

  7. 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试

    原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...

  8. 重新想象 Windows 8 Store Apps (18) - 绘图: Shape, Path, Stroke, Brush

    原文:重新想象 Windows 8 Store Apps (18) - 绘图: Shape, Path, Stroke, Brush [源码下载] 重新想象 Windows 8 Store Apps ...

  9. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解

    [源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...

随机推荐

  1. linux时间方面的设置

    例如以下一段代码能够借鉴: static void _sleep_response_timeout(modbus_t *ctx) { #ifdef _WIN32 /* usleep doesn't e ...

  2. HNCU1099:堆积木

    http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1099 题目描述 小明的侄子要过生日了,小 ...

  3. OpenRisc-32-ORPSoC烧写外部spi flash

    引言 经过前面的分析和介绍,我们对ORPSoC的启动过程(http://blog.csdn.net/rill_zhen/article/details/8855743)和 ORpSoC的debug子系 ...

  4. poj3974(manacher)

    传送门:Palindrome 题意:给定一个字符串,求最长回文子串. 分析:manach裸题,核心理解mx>i?p[i]=min(p[2*id-i],mx-i):1. #pragma comme ...

  5. linux kickstart 自动安装

    最近很多业务系统都是linux lnmp平台安装,反复的安装让人觉得很苦恼,仔细钻研了下kickstart .这里环境是red hat linux 5.8 32位,系统盘中的软件包里包含有kickst ...

  6. Android数据库hibernate框架

    说明 /** * YDL_Hibernate总结 <br/> * (一)支持功能: 1.自己主动建表,支持属性来自继承类:可依据注解自己主动完毕建表,而且对于继承类中的注解字段也支持自己主 ...

  7. sar使用说明

     sar这东西,一开始还以为是内部有的,原来是外部的工具,可以到 http://pagesperso-orange.fr/sebastien.godard/download.html 去下载 1 安装 ...

  8. 将svnkit转成dlls时的问题

    未处理 System.TypeInitializationException Message="“org.tmatesoft.svn.core.internal.wc.DefaultSVNO ...

  9. HDu 2830 Matrix Swapping II(dp)

    Problem Description Given an N * M matrix with each entry equal to 0 or 1. We can find some rectangl ...

  10. 一起来开发Android的天气软件(四)——使用Gson解析数据

    离上一篇文章过去才4.5天,我们赶紧趁热打铁继续完毕该系列的天气软件的开发. 承接上一章的内容使用Volley实现网络的通信.返回给我们的是这一串Json数据{"weatherinfo&qu ...