UWP ListView ItemTemplate缩略图错误

时间:2018-10-17 03:26:00

标签: binding uwp uwp-xaml

我要制作具有缩略图的文件ListView。但是,当我调试此代码时,退出了vs调试器。

XAML

<ListView IsItemClickEnabled="True" x:Name="File_ListView" ItemClick="File_ListView_ItemClick">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Image Grid.RowSpan="2" Grid.Column="0" Width="100" Source="{Binding Thumbnail, Converter={StaticResource ThumbnailToImageConverter}}"/>
                <TextBlock Text="{Binding FileName}" Grid.Row="0" Grid.Column="1"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

代码(C#)

var fileClass = new FileClass()
{
    StorageFile = file,
    Thumbnail = await file.GetScaledImageAsThumbnailAsync(ThumbnailMode.SingleItem),
    FileName = file.DisplayName,
    FileExtension = file.FileType,
    FilePath = file.Path,
};
pictureClass.FileList.Add(fileClass);

FileClass

class FileClass
{
    public StorageFile StorageFile { get; set; }
    public StorageItemThumbnail Thumbnail { get; set; }
    public string FileName { get; set; }
    public string FileExtension { get; set; }
    public string FilePath { get; set; }
}

**转换器**

public class ThumbnailToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        BitmapImage image = null;

        if (value != null)
        {
            if (value.GetType() != typeof(StorageItemThumbnail))
            {
                throw new ArgumentException("Expected a thumbnail");
            }
            if (targetType != typeof(ImageSource))
            {
                throw new ArgumentException("What are you trying to convert to here?");
            }
            StorageItemThumbnail thumbnail = (StorageItemThumbnail)value;
            image = new BitmapImage();
            image.SetSource(thumbnail);
        }
        return (image);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

0 个答案:

没有答案