本小节介绍UWP中的文件操作,使用到了FileOpenPickerAPI(在Windows.Storage.Pickers中)。本例中,单击打开文件按钮,然后在图片库中选择照片,将选择的照片用作贺卡背景。学完本节课程,您能够使用FileOpenPicker来打开设备文件。留个小作业,请探索使用FileSavePicker将文件保存到设备。

MainPage.xaml页面,定位到abtnOpenFile控件,定义单击事件为OpenFile_Click

<AppBarButton x:Name="abtnOpenFile" Icon="Pictures" Label="Open" Click="OpenFile_Click"/>

然后,用鼠标在OpenFile_Click中任何位置单击, 在键盘上按下F12键,进入Code Behind代码。

使用关键字async将OpenFile_Click修改为异步方法。

private async void OpenFile_Click(object sender, RoutedEventArgs e)
{
}

定义FileOpenPicker对象, 将文件浏览位置设置为PickerLocationId.PicturesLibrary, 将浏览文件类型设置为.jpg和.png,代码如下:

FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
fileOpenPicker.FileTypeFilter.Add(".jpg");
fileOpenPicker.FileTypeFilter.Add(".png"); StorageFile photo = await fileOpenPicker.PickSingleFileAsync();

然后,通过文件流方式将StorageFile传给BitmapImage,再将BitmapImage传给ImageBrush,进而设置为gridMsg的背景。

if (photo == null)
return; BitmapImage bitmapImage = new BitmapImage();
using (IRandomAccessStream stream =await photo.OpenAsync(FileAccessMode.Read))
{
bitmapImage.SetSource(stream);
} ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = bitmapImage;
gridMsg.Background = imageBrush;

其中, IRandomAccessStream来自于Windows.Storage.Streams,用于以流的形式访问文件。

M5: 使用StorageFile的更多相关文章

  1. 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件

    [源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...

  2. win10 uwp 从StorageFile获取文件大小

    本文主要:获取文件大小 private async Task<ulong> FileSize(Windows.Storage.StorageFile file) { var size = ...

  3. “IAsyncOperation<StorageFile>”不包含“GetAwaiter”的定义

    错误 CS4036 "IAsyncOperation<StorageFile>"不包含"GetAwaiter"的定义,并且找不到可接受类型为&quo ...

  4. 例子:动能并不是特别强(2-3)后,下M5的同时,也是恢复期到期的前一天

    动能并不是特别强(2-3)后,下M5的同时,但是恢复期到期 EG.002195 2017/06/23-->2017/06/29

  5. 在win8 App中,StorageFile比Path更好用

    Skip the path: stick to the StorageFile: http://blogs.msdn.com/b/wsdevsol/archive/2012/12/05/stray-f ...

  6. UWP Read write File -StorageFile

    // private void MainPage_Loaded(object sender, RoutedEventArgs e) { GetFileAsync(); } public async v ...

  7. 华为云测平台服务再升级!华为M5系列平板调测能力正式上线!

    ​​​6月1日,华为M5系列平板设备兼容性测试和远程真机调试功能在华为终端开放实验室正式上线!助力您的产品在大屏适配上快人一步! 华为终端开放实验室DevEco平台现已提供基于华为M5系列平板设备的兼 ...

  8. UWP StorageFile StorageFolder StorageFileHelper

    //获取表示指定文件系统路径中的文件夹的 StorageFolder. StorageFolder folder1 = await StorageFolder.GetFolderFromPathAsy ...

  9. 【M5】对定制的“类型转换函数”保持警觉

    1.隐式类型转换有两种情况:单个形参构造方法和隐式类型转换操作符.注意:隐式类型转换不是把A类型的对象a,转化为B类型的对象b,而是使用a对象构造出一个b对象,a对象并没有变化. 2.单个形参构造方法 ...

随机推荐

  1. sublime 使用技巧

    使用sublime使遇到的问题: 1.左侧菜单栏隐藏恢复:View ->Side Bar ->Show Side Bar 2.顶部菜单栏隐藏恢复:按住ctrl+shift+p,出现一个框, ...

  2. hadoop2.0初识1.3

    1.配置分布式hadoop 1.1 准备三台测试机(虚拟机就可以) 1.1.1 将life-hadoop虚拟机克隆2个分别为life-hadoop02和life-hadoop03 1.1.2 查看ip ...

  3. Python:C语言扩展

    1. 概述 Python 可以非常方便地和 C 进行相互的调用. 一般,我们不会使用 C 去直接编写一个 Python 的模块.通常的情景是,我们需要把 C 的相关模块包装一下,然后在 Python ...

  4. Sqrt(x) - LintCode

    examination questions Implement int sqrt(int x). Compute and return the square root of x. Example sq ...

  5. c++ 指针常量,常量指针

    当const遇到指针 一般来说,const修饰指针可以分为下面的集中情况. 描述 例子 含义 备注 const在*的左边 const int *b=&a; int const *b=& ...

  6. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. kd tree学习笔记 (最近邻域查询)

    https://zhuanlan.zhihu.com/p/22557068 http://blog.csdn.net/zhjchengfeng5/article/details/7855241 KD树 ...

  8. Effective Objective-C 2.0 学习记录

    由于最近入职,公司安排自由学习,于是有时间将Effective Objective-C 2.0一书学习了一遍.由于个人知识面较窄,对于书中有些内容无法理解透彻,现将所学所理解内容做一遍梳理,将个人认为 ...

  9. table插件实现

    选择.取消.全选.全部取消.获取行ids /** * Created by lizongqiong on 2016/1/8. */ var $ = require('jquery'); var tab ...

  10. Eclipse 最全快捷键

    英文的