3

我想在图片库中显示图片。我得到图片并绑定数据。

StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
IReadOnlyList<StorageFile> myPictures = await picturesFolder.GetFilesAsync();
var mydata = from file in myPictures select new { Subtitle = "subtitle", Title = "title", Image = this.getImage(file.Path) };

this.DefaultViewModel["Items"] = mydata;

这是设置 BitmapImage 的 getImage()。

private async Task<BitmapImage> getImage(string finename)
{
    StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
    StorageFile file = await picturesFolder.GetFileAsync(fileName);
    var stream = await file.OpenReadAsync();

    var bitmap = new BitmapImage();
    bitmap.SetSource(stream);

    return bitmap;
 }

但是图片不显示。我认为这是因为异步功能,但我不知道解决方案。你可以帮帮我吗 ?

4

2 回答 2

2

我不确定您如何使用设置为 的数据DefaultViewModel,但是是的,看起来该async方法是您的问题。

您需要做的是以某种方式await每次调用getImage(). 一种方法是async在你的select. 但要做到这一点,您需要使用方法语法。

当你这样做时,你将拥有IEnumerable<Task<a>>a你的匿名类型在哪里),但你只需要IEnumerable<a>. 为此,请使用Task.WhenAll()(将返回Task<a[]>),然后使用await其结果:

var tasks = myPictures.Select(
    async file => new { Subtitle = "subtitle", Title = "title", Image = await getImage(file.Path) });
var data = await Task.WhenAll(tasks);

这将一次执行所有getImage()s,这可能不是最有效的解决方案。如果您不希望这样,您将需要不同的解决方案。

于 2013-01-20T01:04:26.900 回答
1

svick 的解决方案似乎应该可行,但正如他/她所说 - 它可能不是最有效的解决方案。对于包含未知数量文件的文件夹,更好的解决方案是使用FileInformationFactory.GetVirtualizedFilesVector(). 最好使用转换器。

我用过的东西:

获取虚拟化文件列表并绑定到 ListView

private async void GetPicturesFromGalleryFolder()
{
    var queryOptions = new QueryOptions();
    queryOptions.FolderDepth = FolderDepth.Shallow;
    queryOptions.IndexerOption = IndexerOption.UseIndexerWhenAvailable;
    queryOptions.SortOrder.Clear();

    var sortEntry = new SortEntry {PropertyName = "System.DateModified", AscendingOrder = false};
    queryOptions.SortOrder.Add(sortEntry);
    queryOptions.FileTypeFilter.Add(".png");

    var fileQuery = KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions);
    var fileInformationFactory =
        new FileInformationFactory(
            fileQuery,
            ThumbnailMode.PicturesView,
            0,
            ThumbnailOptions.None,
            true);

    MyListView.ItemsSource = fileInformationFactory.GetVirtualizedFilesVector();
}

XAML

<ListView.ItemTemplate>
    <DataTemplate>
        <Image
            Source="{Binding Converter={StaticResource converters:IStorageItemInformationToBitmapImageConverter}"/>
    </DataTemplate>
</ListView.ItemTemplate>

IStorageItemInformationToBitmapImageConverter

public class IStorageItemInformationToBitmapImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var fileInfo = value as FileInformation;

        if (fileInfo != null)
        {
            var bi = new BitmapImage();

            // The file is being opened asynchronously but we return the BitmapImage immediately.
            SetSourceAsync(bi, fileInfo);
            return bi;

        }

        return null;
    }

    private async void SetSourceAsync(BitmapImage bi, FileInformation fi)
    {
        try
        {
            using (var stream = await fi.OpenReadAsync())
            {
                await bi.SetSourceAsync(stream);
            }
        }
        catch
        {
            // ignore failure
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return null;
    }
}
于 2013-01-20T05:27:12.717 回答