Windows store app[Part 2]:全新的File System与Uri不匹配的问题
在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不匹配的问题的更多相关文章
- 在桌面程序上和Metro/Modern/Windows store app的交互(相互打开,配置读取)
这个标题真是取得我都觉得蛋疼..微软改名狂魔搞得我都不知道要叫哪个好.. 这边记录一下自己的桌面程序跟windows store app交互的过程. 由于某些原因,微软的商店应用的安全沙箱导致很多事情 ...
- Windows Store App 过渡动画
Windows Store App 过渡动画 在开发Windows应用商店应用程序时,如果希望界面元素进入或者离开屏幕时显得自然和流畅,可以为其添加过渡动画.过渡动画能够及时地提示用户屏幕所发 ...
- Windows store app[Part 4]:深入WinRT的异步机制
接上篇Windows store app[Part 3]:认识WinRT的异步机制 WinRT异步机制回顾: IAsyncInfo接口:WinRT下异步功能的核心,该接口提供所有异步操作的基本功能,如 ...
- Windows store app[Part 3]:认识WinRT的异步机制
WinRT异步机制的诞生背景 当编写一个触控应用程序时,执行一个耗时函数,并通知UI更新,我们希望所有的交互过程都可以做出快速的反应.流畅的操作感变的十分重要. 在连接外部程序接口获取数据,操作本地数 ...
- Windows store app[Part 1]:读取U盘数据
Windows 8系统下开发App程序,对于.NET程序员来说,需要重新熟悉下类库. 关于WinRT,引用一张网上传的很多的结构图: 图1 针对App的开发,App工作在系统划定的安全沙箱内,所以通过 ...
- 05、Windows Store app 的图片裁切(更新)
在 Win Phone Silverlight api 中,有一个 PhotoChooserTask 选择器,指定宽.高属性,在选择图片的时候, 可以进行裁切,代码: PhotoChooserTask ...
- 01、Windows Store APP 设置页面横竖屏的方法
在 windows phone store app 中,判断和设置页面横竖屏的方法,与 silverlight 中的 Page 类 不同,不能直接通过 Page.Orientation 进行设置.而是 ...
- Windows Store App JavaScript 开发:获取文件和文件夹列表
在应用程序中有时可能需要获取用户库中的内容,以便执行相关的操作.如果要获取某个用户库中的内容,需要先获取到这个用户库,获得用户库可以通过Windows.Storage命名空间中的KnownFolder ...
- Windows Store App JavaScript 开发:选取文件和文件夹
前面提到过,文件打开选取器由FileOpenPicker类表示,用于选取或打开文件,而文件夹选取器由FolderPicker类表示,用来选取文件夹.在FileOpenPicker类中,pickSing ...
随机推荐
- Excel中通过向导方式插入chart
1.插入图表则主要是操作ChartObject对象和Chart对象. Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet); Workshe ...
- 「小程序JAVA实战」小程序头像图片上传(中)(44)
转自:https://idig8.com/2018/09/09/xiaochengxujavashizhanxiaochengxutouxiangtupianshangchuan43/ 用户可以上传了 ...
- log4j:WARN No appenders could be found for logger 解决办法
转自:https://blog.csdn.net/chw0629/article/details/80567936 使用log4j时不起作用,每次执行完出现以下提示: log4j:WARN No ap ...
- Android apk couldn't install
an existing package with the same name and signature is already installed
- Oracle表结构转Mysql表结构
1. fnc_table_to_mysql 主体程序 create or replace function fnc_table_to_mysql ( i_owner in string, i_tabl ...
- Spring Boot tomcat参数
主题 初学SpringBoot,想要配置一下tomcat的端口,以前tomcat直接在它的XML里配置就好了.现在SpringBoot直接继承了,不知道哪里配置.后来找到解决方法,记录一下. 具体方法 ...
- python:随机数 random
#随机数 import random print(random.randint(10,12))#生成10-12之间的整数 print(random.uniform(10,12))#生成10-12之间的 ...
- LUA 环境
LUA中环境是指一个函数执行的表,即一个函数在什么表中执行. 这里的函数是特殊的,是loadfile("x.lua")的返回值. loadfile("x.lua" ...
- C语言增量内存申请 realloc
void* realloc (void* ptr, size_t size); Reallocate memory block Changes the size of the memory block ...
- Git 版本导致 clone 故障
问题描述: git clone 报错如下: Initialized empty Git repository in /root/project_php/.git/ error: The request ...