在Win 8 App的安全沙箱内,除了使用文件选取器FileOpenPicker外,没有其他办法调用某个盘符的数据。

全新的Storage命名空间,借鉴了IOS与Android的设计。

下面引用一个图片绑定的简单例子:

原来WPF我们可以这样写:

     <Grid Background="Red">
<Image x:Name="bg1" Source="ms-appx:///Assets/shanghai.jpg"></Image>
</Grid>

也可以在Code-Behind这样写:

this.DataContext = new BitmapImage(new Uri("ms-appx:///Assets/shanghai.jpg"));

XAML:

 <Image x:Name="bg1" Source="{Binding}"></Image>

效果都是这样:

下面我们用Win8 App的Storage来重写一下上面的代码。

为了举例,我们需要引用本地图片库的图片文件。

在AppPackage.appxmanifest中,加上配置

   <Capabilities>
<Capability Name="picturesLibrary" />
</Capabilities>

Code-Behind:

             var file = KnownFolders.PicturesLibrary.GetFileAsync("shanghai.jpg").AsTask().Result;

             this.DataContext = new BitmapImage(new Uri(file.Path));

运行程序,图片并没有显示,这说明Storage是不支持Uri的。

解决方案1:通过读取File的Stream,Set Image的Source,代码如下:

             var file = await KnownFolders.PicturesLibrary.GetFileAsync("shanghai.jpg");
using (var fileStream = await file.OpenAsync(FileAccessMode.Read)) {
BitmapImage image = new BitmapImage();
image.SetSource(fileStream);
this.DataContext = image;
}

解决方案2:通过WriteBitmap类实现,代码如下:

         async private void GetImage() {
var file = await KnownFolders.PicturesLibrary.GetFileAsync("shanghai.jpg");
using (var stream = await file.OpenAsync(FileAccessMode.Read)) {
BitmapImage image = new BitmapImage();
image.SetSource(stream);
stream.Seek();
backgroundBmp = new WriteableBitmap(image.PixelWidth, image.PixelHeight);
await backgroundBmp.SetSourceAsync(stream);
this.DataContext = backgroundBmp;
}
} private WriteableBitmap backgroundBmp;

同样,也可以通过引用WriteableBitmapEx.WinRT简化写法,代码如下:

         async private void GetImage() {
var file = await KnownFolders.PicturesLibrary.GetFileAsync("shanghai.jpg");
using (var stream = await file.OpenAsync(FileAccessMode.Read)) {
BitmapImage image = new BitmapImage();
image.SetSource(stream);
stream.Seek();
backgroundBmp = await BitmapFactory.New(image.PixelWidth, image.PixelHeight).FromStream(stream);
this.DataContext = backgroundBmp;
}
} private WriteableBitmap backgroundBmp;

解放方案1和2的区别,WriteableBitmap可以精确指定生成的Image的区域,这样就可以通过WriteableBitmap实现图片的缩放,裁剪。

代码:

Windows store app[Part 2]:全新的File System与Uri不匹配的问题的更多相关文章

  1. 在桌面程序上和Metro/Modern/Windows store app的交互(相互打开,配置读取)

    这个标题真是取得我都觉得蛋疼..微软改名狂魔搞得我都不知道要叫哪个好.. 这边记录一下自己的桌面程序跟windows store app交互的过程. 由于某些原因,微软的商店应用的安全沙箱导致很多事情 ...

  2. Windows Store App 过渡动画

    Windows Store App 过渡动画     在开发Windows应用商店应用程序时,如果希望界面元素进入或者离开屏幕时显得自然和流畅,可以为其添加过渡动画.过渡动画能够及时地提示用户屏幕所发 ...

  3. Windows store app[Part 4]:深入WinRT的异步机制

    接上篇Windows store app[Part 3]:认识WinRT的异步机制 WinRT异步机制回顾: IAsyncInfo接口:WinRT下异步功能的核心,该接口提供所有异步操作的基本功能,如 ...

  4. Windows store app[Part 3]:认识WinRT的异步机制

    WinRT异步机制的诞生背景 当编写一个触控应用程序时,执行一个耗时函数,并通知UI更新,我们希望所有的交互过程都可以做出快速的反应.流畅的操作感变的十分重要. 在连接外部程序接口获取数据,操作本地数 ...

  5. Windows store app[Part 1]:读取U盘数据

    Windows 8系统下开发App程序,对于.NET程序员来说,需要重新熟悉下类库. 关于WinRT,引用一张网上传的很多的结构图: 图1 针对App的开发,App工作在系统划定的安全沙箱内,所以通过 ...

  6. 05、Windows Store app 的图片裁切(更新)

    在 Win Phone Silverlight api 中,有一个 PhotoChooserTask 选择器,指定宽.高属性,在选择图片的时候, 可以进行裁切,代码: PhotoChooserTask ...

  7. 01、Windows Store APP 设置页面横竖屏的方法

    在 windows phone store app 中,判断和设置页面横竖屏的方法,与 silverlight 中的 Page 类 不同,不能直接通过 Page.Orientation 进行设置.而是 ...

  8. Windows Store App JavaScript 开发:获取文件和文件夹列表

    在应用程序中有时可能需要获取用户库中的内容,以便执行相关的操作.如果要获取某个用户库中的内容,需要先获取到这个用户库,获得用户库可以通过Windows.Storage命名空间中的KnownFolder ...

  9. Windows Store App JavaScript 开发:选取文件和文件夹

    前面提到过,文件打开选取器由FileOpenPicker类表示,用于选取或打开文件,而文件夹选取器由FolderPicker类表示,用来选取文件夹.在FileOpenPicker类中,pickSing ...

随机推荐

  1. python中的json的基本使用方法

    在python中使用json的时候,主要也就是使用json模块,json是以一种良好的格式来进行数据的交互,从而在很多时候,可以使用json数据格式作为程序之间的接口, #!/usr/bin/env ...

  2. leetcode529

    public class Solution { //DFS public char[,] UpdateBoard(char[,] board, int[] click) { ), n = board. ...

  3. 搭建一个Web API项目(DDD)

    传送阵:写在最后 一.创建一个能跑的起来的Web API项目 1.建一个空的 ASP.NET Web应用 (为什么不直接添加一个Web API项目呢,那样会有些多余的内容(如js.css.Areas等 ...

  4. 使apk具有system权限

    使apk具有system权限的方法:   方法一:   1. 在应用程序的AndroidManifest.xml中的manifest节点中加入   android:sharedUserId=" ...

  5. Ant学习--简单实例入门

    步骤1.创建工程文件夹名字 F:\Helloworld 步骤2.编写java代码: package com.netease; public class Ant_test { public static ...

  6. vmware centos7 静态ip设置

    最近在学习linux环境部署~~~~ 首先,将网络适配设置成为桥接模式 查看本机IP地址,ipconfig,记住ipv4地址和默认网关地址,等会配置的时候要用 启动Centos,进入终端模式,设置IP ...

  7. Openssl s_client命令

    一.简介 s_client为一个SSL/TLS客户端程序,与s_server对应,它不仅能与s_server进行通信,也能与任何使用ssl协议的其他服务程序进行通信 二.语法 openssl s_cl ...

  8. loadrunner录制时web时,ie报安全证书问题

    解决方法:在Recording_Options下Port Mapping>Capture level设置为 WinNet level data Capture Level的设置说明:1.Sock ...

  9. LIS和LCS LCIS

    首先介绍一下LIS和LCS的DP解法O(N^2) LCS:两个有序序列a和b,求他们公共子序列的最大长度 我们定义一个数组DP[i][j],表示的是a的前i项和b的前j项的最大公共子序列的长度,那么由 ...

  10. 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程

    在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也 ...