Metro / Windows 8 + C#/ XAML:如何找到存储在文档库中的图片的有效URI? [英] Metro/Windows 8 + C#/XAML: How to find valid URIs to pictures stored in the Documents Library?

查看:169
本文介绍了Metro / Windows 8 + C#/ XAML:如何找到存储在文档库中的图片的有效URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Windows 8,我创建了一个应用程序,可以从网上下载图片,并以程序创建的子文件夹存储在用户的文档库中。



所需的功能和声明到应用程序清单:
$ b


  • 文档库作为一个功能被启用

  • 文件类型添加了jpg的关联,并在声明中标记了open is safe



启动应用程序时,所有文件夹和文件都被正确创建 - 用户可以从文件系统手动打开它们。

我的挑战如下:当我以编程方式加载所有可用的图像并将这个图像列表绑定到XAML元素时,我需要一个URIxxx:///yyy/zzz.jpg,可以用来正确设置图像源。我尝试了各种模式的风味,包括。 file:///yyy/zzz.jpg但没有成功。



有没有办法做到这一点,或者根本没有办法?

应用程序数据,XAML很喜欢像ms-appdata:///local/zzz.jpg这样的URI。但我绝对宁愿将图片存储在用户的图书馆中。



感谢您的任何帮助!

解决方案

如果您绑定到图像源属性,则无法绑定文件路径。您必须绑定到BitmapImage数据类型。如果您直接在代码中设置Source属性,它将如下所示:

  BitmapImage bitmapImage = new BitmapImage(); 
等待bitmapImage.SetSourceAsync(fileStream);
MyImage.Source = bitmapImage;

如果绑定到一个对象列表,那么这些对象需要有一个属性, BitmapImage数据类型(简化 - 您还需要支持 INotifyPropertyChanged ):

  public class MyDataBoundItem 
{
public BitmapImage MyBindableImage {get; set;}
}

你可以像这样的代码枚举图片文件夹中的文件...

  var picFolder = Windows.Storage.KnownFolders.PicturesLibrary; 
var options = new QueryOptions(CommonFileQuery.DefaultQuery,new List< string> {.png,.jpg});
options.FolderDepth = FolderDepth.Deep;

var query = picFolder.CreateFileQueryWithOptions(options);
var files =等待query.GetFilesAsync();

这个技巧将取决于你想要加载数据的方式,相应的文件从文件到每个数据绑定对象,以便它可以提供BitmapImage属性。这是因为SetSourceAsync调用是异步的,您将需要支持INotifyPropertyChanged,以便绑定的对象可以在图像可用时通知UI。不难,只是不直观明显。


For Windows 8, I have created an app that downloads pictures from the web and stores them in programmatically created subfolders in the user's Document Library.

I have added the needed capabilities and declarations to the application manifest:

  • Documents Library enabled as a capability
  • File type association for jpg added and "open is safe" marked in declarations

When firing up the app, all folders and files are created correctly - and the user can open them manually from the file system.

My challenge is the following: when I programmatically load all available images and bind this list of images to XAML elements, I need a URI "xxx:///yyy/zzz.jpg" that can be used to set the image source correctly . I tried various schema flavours incl. "file:///yyy/zzz.jpg" but none succeeded.

Is there any way of doing this or is there no way at all?

When pictures are stored in the application data, XAML is happy with URIs like "ms-appdata:///local/zzz.jpg". But I definitely I would prefer to store pictures in the user's libraries.

Thanks for any help!

解决方案

If you are binding to the Image Source property, you cannot bind a file path. You must bind to a BitmapImage data type. If you were setting the Source property directly in code, it would look like this:

           BitmapImage bitmapImage = new BitmapImage();
            await bitmapImage.SetSourceAsync(fileStream);
            MyImage.Source = bitmapImage;

If you are binding to an object list, then the objects need to have a property on them that returns the BitmapImage data type (simplifying - you would need to support INotifyPropertyChanged as well):

public class MyDataBoundItem
{
   public BitmapImage MyBindableImage {get;set;}
}

You can enumerate the files in the Pictures folder with code like this...

var picFolder = Windows.Storage.KnownFolders.PicturesLibrary;
var options = new QueryOptions(CommonFileQuery.DefaultQuery,new List<string> { ".png", ".jpg" }) ;
options.FolderDepth = FolderDepth.Deep;

var query = picFolder.CreateFileQueryWithOptions(options);
var files = await query.GetFilesAsync();

The trick will be, and it depends on how you want to load your data, is to get the corresponding file from files into each of your databound objects so that it can provide the BitmapImage property. It's because the SetSourceAsync call being async that you will need to support INotifyPropertyChanged so the bound object can notify the UI when the image is available. Not hard, just not intuitively obvious.

这篇关于Metro / Windows 8 + C#/ XAML:如何找到存储在文档库中的图片的有效URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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