【问题标题】:Populating ListView by Images from the Pictures Library (Windows Phone 8.1)通过图片库中的图像填充 ListView (Windows Phone 8.1)
【发布时间】:2016-03-22 20:09:54
【问题描述】:

所以我可以使用以下语法一次一张地填充 ListView:

var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(await file.OpenReadAsync());
PhotoListView.Items.Add(bitmap);

但是我希望我的 ListView 显示图片库中特定文件夹中的所有照片,例如相机胶卷文件夹。执行与上述相同的操作,并添加允许我使用 StorageFolder 打开文件夹的代码给我一个错误:

failed to convert value of type 'Windows.Storage.StorageFile' to type 'ImageSource'

我认为它与 List 有关,但我不知道如何使用它。顺便说一句,我也可以考虑使用 FilePicker,只要我可以在 我的应用程序中而不是在图片库本身中查看所有图像。这是因为我已经看到了它的示例,它的作用是将应用程序置于后台并打开库以显示图像,这对我没有好处。

所以我的问题是,我该怎么做呢?它背后的代码是什么?请帮我。谢谢!

【问题讨论】:

  • 文件夹可以包含任何类型的文件:pdf、txt、doc 等。您不能将文件夹转换为图像列表;想一想,如果有一个txt文件不能转成图片,那怎么办?

标签: c# xaml listview windows-phone-8.1


【解决方案1】:

正如预期的那样,它必须与列表有关。所以我研究了很多,然后想出了这个代码:

StorageFolder appFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync("appFolder", CreationCollisionOption.OpenIfExists);
StorageFolder temp = await appFolder.CreateFolderAsync("temp", CreationCollisionOption.OpenIfExists);

List<StorageFile> FileList = (await temp.GetFilesAsync()).ToList();

List<string> ImageList = new List<string>();

    foreach (var imageFile in FileList)
        {
            ImageList.Add(imageFile.Path);
        }

this.PhotoListView.DataContext = ImageList;

ListView 也使用绑定,因此我的 appFolder 中的图像将自动添加到其中。

【讨论】: