wp8.1 Study14 FilePicker简单介绍
一、FileOpenPicker/FileSavePicker介绍
这个在使用手机中是十分经常的,如在朋友圈中你要发图片,首先点击添加图片,而后你会进入手机图片区,选择图片后自动返回朋友圈准备发图界面。简单的步骤演示如下图所示:
它允许App获取手机中任何类型的文件,可以不通过KnownFoldersAPI从Pictures库及Vedio库获得相应文件。
二、简单例子
在页面中,放入一个Image控件及一个叫Pick的Button控件,假设想点击pick,然后进入选择图片库,选择一个图片,然后Image控件显示相应选择的图片。
pick点击事件C#代码如下:
private void pick_Click(object sender, RoutedEventArgs e)
{
//创造一个picker对象
var file = new FileOpenPicker();
//picker打开文件类型
file.FileTypeFilter.Add(".jpg");
file.FileTypeFilter.Add(".jpeg");
file.FileTypeFilter.Add(".png");
//打开picker获取一个文件
file.ContinuationData["Operation"] = "UpdateProfilePicture";//这个为了后面保存图片的操作
file.PickSingleFileAndContinue();
}
Pick 一个文件后,还需要在App.xaml.cs重写Activate方法,代码如下:
protected override void OnActivated(IActivatedEventArgs args)
{
if (args is FileOpenPickerContinuationEventArgs)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
// 创建要充当导航上下文的框架,并导航到第一页
rootFrame = new Frame(); // TODO: 将此值更改为适合您的应用程序的缓存大小
rootFrame.CacheSize = ;
Window.Current.Content = rootFrame;
} // Standard Frame initialization code ... if (!rootFrame.Navigate(typeof(Page1)))
{
throw new Exception("Failed to create target page");
} var p = rootFrame.Content as Page1;
p.FilePickerEvent = (FileOpenPickerContinuationEventArgs)args; // Ensure the current window is active
Window.Current.Activate();
}
}
最后还需要在页面中加入一下代码
private FileOpenPickerContinuationEventArgs _filePickerEventArgs = null;
public FileOpenPickerContinuationEventArgs FilePickerEvent
{
get { return _filePickerEventArgs; }
set
{
_filePickerEventArgs = value;
ContinueFileOpenPicker(_filePickerEventArgs);
}
} private async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs _filePickerEventArgs)
{
if ((_filePickerEventArgs.ContinuationData["Operation"] as string) == "UpdateProfilePicture" && _filePickerEventArgs.Files != null && _filePickerEventArgs.Files.Count > )
{
StorageFile file = _filePickerEventArgs.Files[];
string mrutoken = StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "");//mrucache保存选择图片记录
IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(fileStream);
imageshow.Source = bitmapImage; }
注:还可以使用SavePicker ,使用如上,代码如下:
//Create the picker object
FileSavePicker savePicker = new FileSavePicker(); // Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add(
"Plain Text", new List<string>() { ".txt" });
// Default file name if the user does not type one in or select // a file to replace
savePicker.SuggestedFileName = "New Document"; // Open the picker for the user to pick a file
savePicker.ContinuationData["Operation"] = "SomeDataOrOther";
savePicker.PickSingleFileAndContinue();
三、Windows.Storage.AccessCache
这是让使用者能快速重新打开之前打开过的文件的缓存,我们可以使用FutureAccessList 和 MostRecentlyUsedList,其中MostRecentlyUsedList是一个记录着经常使用的文件的列表,而FutureAccessList是一个可以保存文件以便以后的使用的列表。当使用者pick一个文件等,App需要把其加入以上两个列表记录。
在 ContinueFileOpenPicker()方法添加
// Add to MRU with metadata (For example, a string that represents the date)
string mruToken = StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "");
// Add to FA without metadata
string faToken = StorageApplicationPermissions.FutureAccessList.Add(file);
而每当需要使用列表时,可以这样调用代码:
// get the token for the first item in our MRU and use it to retrieve a StorageFile that represents that file
String mruFirstToken = StorageApplicationPermissions.MostRecentlyUsedList.Entries.First().Token;
StorageFile retrievedFile = await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(mruFirstToken);
... // Retrieve tokens for all items in the MRU
AccessListEntryView mruEntries = StorageApplicationPermissions.MostRecentlyUsedList.Entries;
if (mruEntries.Count > )
{
foreach (AccessListEntry entry in mruEntries)
{
String mruToken = entry.Token;
// Continue processing the MRU entry
...
}
}
wp8.1 Study14 FilePicker简单介绍的更多相关文章
- [原创]关于mybatis中一级缓存和二级缓存的简单介绍
关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...
- 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍
一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...
- 利用Python进行数据分析(4) NumPy基础: ndarray简单介绍
一.NumPy 是什么 NumPy 是 Python 科学计算的基础包,它专为进行严格的数字处理而产生.在之前的随笔里已有更加详细的介绍,这里不再赘述. 利用 Python 进行数据分析(一)简单介绍 ...
- yii2的权限管理系统RBAC简单介绍
这里有几个概念 权限: 指用户是否可以执行哪些操作,如:编辑.发布.查看回帖 角色 比如:VIP用户组, 高级会员组,中级会员组,初级会员组 VIP用户组:发帖.回帖.删帖.浏览权限 高级会员组:发帖 ...
- angular1.x的简单介绍(二)
首先还是要强调一下DI,DI(Denpendency Injection)伸手获得,主要解决模块间的耦合关系.那么模块是又什么组成的呢?在我看来,模块的最小单位是类,多个类的组合就是模块.关于在根模块 ...
- Linux的简单介绍和常用命令的介绍
Linux的简单介绍和常用命令的介绍 本说明以Ubuntu系统为例 Ubuntu系统的安装自行百度,或者参考http://www.cnblogs.com/CoderJYF/p/6091068.html ...
- iOS-iOS开发简单介绍
概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的IOS程序.但是这里我想强调一下,前面的 ...
- iOS开发多线程篇—多线程简单介绍
iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...
- iOS开发UI篇—UITabBarController简单介绍
iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...
随机推荐
- a++累加
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...
- Selected SVN connector library is not available or cannot be loaded
1.错误描述 The following data will be sent: ------ STATUS ------ pluginId org.eclipse.team.sv ...
- eclipse如何调试(Debug)程序(zhuan)
http://jingyan.baidu.com/article/e6c8503c7e46b6e54f1a18c5.html ************************************* ...
- hiho_99_骑士问题
题目大意 给定国际象棋8x8棋盘上三个起始点,三个骑士分别从三个起始点开始移动(骑士只能走日字,且骑士从任意一点出发可以走遍整个棋盘).现要求三个骑士汇聚到棋盘上某个点,且使得骑士到达该点所移动的次数 ...
- STM8s在利用库配置端口的小问题
在应用的时候PA2口需要设置成推挽输出,控制一个外部电源开关,端口初始化程序如下: GPIO_DeInit(GPIOA); GPIO_Init(GPIOA,GPIO_PIN_2,GPIO_MODE_O ...
- entity reference在views中的运用
一个views block可以获取从url过来的nid,某个node的有entity reference字段, 填入一点数据,然后在views里关联一下这个entity reference field ...
- 百度编辑器 无法获取post过去的值
如果你的,表单form是套用 table的话,请一下,要把 form 放在table的最外面.否则获取不到post值
- java 集合(Map2)
Map 接口的迭代方法: import java.util.*; public class ex12 { public static void main(String[] args) { Map< ...
- Python顺序集合之 tuple
慕课网<Python 入门>学习笔记 1.tuple特性 tuple是另一种有序的列表,中文翻译为“ 元组 ”.tuple 和 list 非常类似,但是,tuple一旦创建完毕,就不能修改 ...
- springmvc:BeanNameViewResolver访问内部资源视图对象和访问外部资源视图对象
<!-- 处理器映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerM ...