UWP - 应用程序文件夹中的图像 Uri [英] UWP - Image Uri in Application Folder

查看:38
本文介绍了UWP - 应用程序文件夹中的图像 Uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示图片时遇到了一个小问题.

因此,当我尝试从 XAML 加载图像时,我可以像这样使用图像源的相对 uri:

<Image Source="/Assets/image.jpg"/>

但是如果我尝试从后面的代码中以编程方式更改图像源,我总是会遇到异常,而且我确定这是因为错误的 Uri.我试过这样的事情:

BitmapImage bitmapImage = new BitmapImage(new Uri("/Assets/image.jpg"));

我做错了吗?任何帮助将不胜感激,谢谢!

解决方案

要访问存储在应用程序包内的文件,但从没有推断出root权限的代码中,您需要指定ms-appx:方案:

所以在你的情况下它会是这样的:

BitmapImage bitmapImage =new BitmapImage(new Uri("ms-appx:///[project-name]/Assets/image.jpg"));

阅读此文档了解更多详情:https:///msdn.microsoft.com/en-us/library/windows/apps/xaml/hh965322.aspx

希望有帮助.

I'm having a little issue here in showing images.

So when I'm trying to load images from XAML, I can use a relative uri to the image source like this :

<Image Source="/Assets/image.jpg" />

But if I try to change the image source programatically from code behind, I always get an exception and I'm sure it's because of the false Uri. I tried something like this :

BitmapImage bitmapImage = new BitmapImage(new Uri("/Assets/image.jpg"));

Am I doing it wrong? Any help will be appreciated, thanks!

解决方案

To access files stored inside the application package, but from code where there is no inferred root authority, you need to specify the ms-appx: scheme :

So in your case it will be something like :

BitmapImage bitmapImage = 
                     new BitmapImage(new Uri("ms-appx:///[project-name]/Assets/image.jpg"));

Read this documentation for more details : https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh965322.aspx

Hope it helps.

这篇关于UWP - 应用程序文件夹中的图像 Uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆