本小节介绍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. [问题2014A12] 复旦高等代数 I(14级)每周一题(第十四教学周)

    [问题2014A12]  设 \(A,B\) 是 \(n\) 阶方阵且满足 \(AB=BA=0\), \(\mathrm{r}(A)=\mathrm{r}(A^2)\), 证明: \[\mathrm{ ...

  2. CSS3的chapter2

    CSS的选择符有很多,大致分为八种: 通配选择符 元素选择符 群组选择符 关系选择符 id及class类选择符 伪类选择符 属性选择符 伪对象选择符 1.通配选择符: 可以使用模糊指定的方式来对对象进 ...

  3. git 临时记录

    http://blog.csdn.net/wangbole/article/details/8552808 http://blog.csdn.net/gq414047080/article/detai ...

  4. ie6兼容之绝对定位元素内容为空时高度问题

    正常显示: ie6下显示: line6元素高度最小16px; 解决办法: 添加内容在空的div里,并且设置行高即可. 其中,非ie6浏览器不需要再空的div里加无谓的内容,再次需要用“条件注释”来解决 ...

  5. CoreText原理及基本使用方法

    关于富文本的排版也是现在的一个技术点,以下是近日关于CoreText的学习记录以及个人理解,希望能对正在学习CoreText的朋友起到帮助. 1.框架坐标系 首先让我们先来看看CoreText坐标系和 ...

  6. struts(三) ---OGNL的学习和理解

    OGNL:Object graphic Navgation Language(对象图形的导航语言)

  7. python uuid、hex study

    由 import uuid product[“SourceInfo"]["ProductID"] = uuid.uuid4().hex 引起的uuid 一.概述 uuid ...

  8. Oracle的表空间和数据文件

    一. 概念 表空间:是一个或多个数据文件的逻辑集合 表空间逻辑存储对象: 永久段-->如表与索引 临时段-->如临时表数据与排序段 回滚段-->用于事物回滚或闪回内存的撤销数据 表空 ...

  9. Java后台传前台json数组

    function checkStore(){ var flag=1; $.ajax({ url:"widget?type=shop_cart&ajax=yes&action= ...

  10. [原创] WINDOWS 7 精简教程之驱动精简 可用于64和32

    追风神话 发表于 2014-9-1 11:35:56   https://www.itsk.com/forum.php?mod=viewthread&tid=334491&highli ...