通过图片库中的图像填充 ListView (Windows Phone 8.1)
Populating ListView by Images from the Pictures Library (Windows Phone 8.1)
所以我可以通过使用以下语法一次一张地填充 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,只要我可以在我的应用程序 中查看所有图像 而不是在图片库本身中。因为我看过它的例子,它的作用是把应用程序放在后台,然后打开库来显示图像,这对我来说不好。
所以我的问题是,我该怎么做呢?它背后的代码是什么?请帮我。谢谢!
果然和列表有关。所以我研究了很多然后想出了这个代码:
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 中的图像将自动添加到其中。
所以我可以通过使用以下语法一次一张地填充 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,只要我可以在我的应用程序 中查看所有图像 而不是在图片库本身中。因为我看过它的例子,它的作用是把应用程序放在后台,然后打开库来显示图像,这对我来说不好。
所以我的问题是,我该怎么做呢?它背后的代码是什么?请帮我。谢谢!
果然和列表有关。所以我研究了很多然后想出了这个代码:
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 中的图像将自动添加到其中。