无法在本地包的gridview数据绑定上显示图像

时间:2017-05-12 02:32:53

标签: c# image gridview uwp

我有一个包含标题和书籍封面的数据绑定的gridview。封面书是在本地包装中的路径名称:" files / kurikulum 2006 / [用户在上一页选择的文件夹的名称] /cover"。我无法显示书的封面,以便不显示书籍封面,错误信息如下所示: error message

但是标题可以显示在gridview上。

XAML:

<GridView
                x:Name="itemGridView"
                AutomationProperties.AutomationId="ItemGridView"
                AutomationProperties.Name="Grouped Items"
                Margin="0,0,10,0"
                HorizontalAlignment="Center"
                ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
                SelectionMode="None"
                IsSwipeEnabled="false"
                IsItemClickEnabled="True"
                ItemClick="ItemView_ItemClick" BorderThickness="0">

                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Grid Height="315" Width="200" Margin="5,10,0,0" Background="White">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>

                            <Image Grid.Row="0" Margin="10,10,10,10" Height="230" Width="180" Source="{Binding Image}" AutomationProperties.Name="{Binding Name}" />

                            <Grid Grid.Row="1" Margin="0,0,10,10" HorizontalAlignment="Left" Height="50" >
                                <ScrollViewer Margin="10,10,5,5" VerticalAlignment="Top" HorizontalAlignment="Left" Height="40" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
                                    <TextBlock Margin="0,0,10,10" Text="{Binding Name}" Foreground="Black" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="Segoe UI Black" FontWeight="SemiBold" TextAlignment="Center" TextWrapping="Wrap" Height="40" Width="auto"/>
                                </ScrollViewer>

                            </Grid>
                        </Grid>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>

代码:

ObservableCollection<Book> datasource = new ObservableCollection<Book>();
            StorageFolder _folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            _folder = await _folder.GetFolderAsync("files");
            _folder = await _folder.GetFolderAsync(direktori.Direktori);
            _folder = await _folder.GetFolderAsync(direktori.Name);
            _folder = await _folder.GetFolderAsync("cover");
            IReadOnlyList<StorageFile> _files = await _folder.GetFilesAsync(); //which returns List<StorageFile>
            foreach (StorageFile file in _files)
            {
                Book buku = new Book();
                buku.Name = file.DisplayName.ToString();
                StorageFile thumbFile;
                thumbFile = await _folder.GetFileAsync(file.DisplayName.ToString() + ".jpg");
                BitmapImage bi = new BitmapImage();
                bi.SetSource(await thumbFile.OpenAsync(FileAccessMode.Read));
                buku.Image = bi;

                datasource.Add(buku);
            }
            itemGridView.ItemsSource = datasource;

图书课程:

public class Book
    {
        public string Name { get; set; }

        public string Direktori { get; set; }

        public ImageSource Image { get; set; }
    }

如何处理?

注意:

direktori.Direktori = kurikulum 2006(或其他文件夹)

direktori.Name =用户在上一页选择的文件夹名称

0 个答案:

没有答案