在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. delphi IOS 通知 TNotification

    delphi  IOS 通知 TNotification http://blogs.embarcadero.com/ao/2013/05/01/39450 TNotification http://d ...

  2. Spring boot + Gradle + Eclipse打war包发布总结

    首先感谢两位博主的分享 http://lib.csdn.net/article/git/55444?knId=767 https://my.oschina.net/alexnine/blog/5406 ...

  3. js将秒转换为 分:秒 函数

    /** * 将秒转换为 分:秒 * s int 秒数 */ function s_to_hs(s){ //计算分钟 //算法:将秒数除以60,然后下舍入,既得到分钟数 var h; h = Math. ...

  4. Mysql事务及行级锁

    事务隔离级别 数据库事务隔离级别,只是针对一个事务能不能读取其它事务的中间结果. Read Uncommitted (读取未提交内容) 在该隔离级别,所有事务都可以看到其他未提交事务的执行结果.本隔离 ...

  5. 关于setTimeout()你所不知道的地方

    前言:看了这篇文章,1.注意setTimeout引用的是全部变量还是局部变量了,当直接调用外部函数方法时,实际上函数内部的变量已经变成全 局.2.提醒我防止出错的,用匿名函数不容易出错.3.setTi ...

  6. Notepad++ 多行一起编辑

      快捷方法: 鼠标:alt+滑鼠左鍵拖拉選取.鍵盤:alt+shift+方向鍵.

  7. windows server R2 密钥

    一.win2012r2激活码 永久激活 Volume版 Windows Server 2012 R2 Datacenter数据中心版: [Key]:TVNTG-VFJQ3-FQXFP-DVCP6-D3 ...

  8. HttpServletResponse返回页面弹窗

    下载方法: @RequestMapping(value = "/download.htm") public void downLoadFile(String id,HttpServ ...

  9. C# ValueTypes

    [C# ValueTypes] 1.哪些类型是ValueType? The value types consist of two main categories: Structs Enumeratio ...

  10. 配置siebel捕捉SQL语句

    C:\Siebel\15.0.0.0.0\Client\BIN\siebel.exe /c c:\Siebel\15.0.0.0.0\Client\bin\chs\siebel.cfg /B &quo ...