本小节介绍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. Balsamiq Mockups 注册码

    Blacklist: Organization name: Rick DongSerial Key: eNrzzU/OLi0odswsqgnKTM5WcMnPS1eoMTQyMjexMDQyAIEa5 ...

  2. ArrayList&LinkedList&Map&Arrays

    Java集合框架 1:集合接口 1.1:Collection接口 Collection接口是构造集合框架的基础.它声明所有类集合都将拥有的核心方法 Boolean add(Object obj) 将o ...

  3. 欧姆龙PLC---FINS/TCP

    ETN 21 以太网fins/tcp命令 (1)将电脑和PLC设置为同一个网段 例如电脑IP为192.168.18.214,PLC的IP为192.168.18.4(PLC的端口默认为9600) (2) ...

  4. linux 如何开机自动运行sh脚本

    vi /etc/rc.d/rc.local #自动启动oracleecho 502 >/proc/sys/vm/hugetlb_shm_group su - oracle -c 'sh /dat ...

  5. Moving From Top To Bottom in Detailed Block in Oracle Forms

    Suppose you want to scan a tabular grid block (loop through all records in detail block) from top to ...

  6. Kanzi Studio中的概念

    Kanzi Studio是Kanzi的UI编辑器,功能非常强大.在使用Kanzi Stadio之前,首先要先熟悉编辑器中的概念. Kanzi Studio中主要分project窗格,property窗 ...

  7. 逻辑操作符---Lua: and,or,not 对比 C++:&&,||,!

    lua中有三个逻辑操作符:and,or,not(逻辑与,逻辑或,逻辑非),同样c++也有类似的三个逻辑操作符:&&,||,!(逻辑与,逻辑或,逻辑非).他们的运算对象就是真和假.lua ...

  8. 【Android】设置 LinearLayout 的样式

    前言 LinearLayout是最常用的控件之一,主要是用来进行排版布局,本人介绍如何给LinearLayout 增加边框样式,在增加样式之前的效果如下: 可以看得出来,每个LinearLayout几 ...

  9. Codeforces Round #263 (Div. 1)

    B 树形dp 组合的思想. Z队长的思路. dp[i][1]表示以i为跟结点的子树向上贡献1个的方案,dp[i][0]表示以i为跟结点的子树向上贡献0个的方案. 如果当前为叶子节点,dp[i][0] ...

  10. 关于 RxJava 技术介绍

    Awesome-RxJava RxJava resources Blog 给 Android 开发者的 RxJava 详解 -强烈推荐 扔物线的文章 讲解非常详细 NotRxJava懒人专用指南 -这 ...