简体   繁体   English

C# UWP 显示图像

[英]C# UWP displaying Image

I have a little problem.我有一个小问题。 I have to display Images from PicturesLibrary.我必须显示图片库中的图像。 I have this code:我有这个代码:

    info = new ImageFileInfo() { ImageTitle = file.Path, ImageType = file.DisplayType, Image = file.Path };

I tried using converter:我尝试使用转换器:

  return new Uri("ms-appx:///" + imagename, UriKind.Absolute);

But my images dont show up.但我的图像不显示。 I know that there is a way to display images from outside of projects, cause I have been using it some time ago, but I forgot how it was done, and I can't find it anywhere.我知道有一种方法可以显示来自项目外部的图像,因为我前段时间一直在使用它,但我忘记了它是如何完成的,而且我在任何地方都找不到它。

The Uri schemes of ms-appx are only used for the files in the project, and the files in the picture library cannot be obtained through it. ms-appx的Uri方案只用于项目中的文件,无法通过它获取图片库中的文件。

In UWP, we do not recommend using the file path to get file.在 UWP 中,我们不建议使用文件路径获取文件。 Because UWP has strict restrictions on accessing files from the path.因为 UWP 对从路径访问文件有严格的限制。

If you want to access files from the picture library, first you need to check Picture Library in the Capability tab in package.appxmenifest.如果要访问图片库中的文件,首先需要在 package.appxmenifest 中的 Capability 选项卡中检查Picture Library

Then you can access it by file name:然后你可以通过文件名访问它:

var img = await KnownFolders.PicturesLibrary.TryGetItemAsync("test.png");

But there are other more recommended way, like use Token to access files:但是还有其他更推荐的方式,比如使用 Token 访问文件:

After obtaining the file, we store it in the future access list and obtain a Token获取文件后,我们将其存储在未来访问列表中,并获取一个Token

Save节省

string faToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
info = new ImageFileInfo() { ImageTitle = file.Path, ImageType = file.DisplayType, Image = faToken };

Get得到

var image = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(info.Image);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM