[UWP] [C#]无法在本地包的gridview数据绑定上显示图像 [英] [UWP] [C#] Cannot display image on gridview data binding from local package
问题描述
我有一个包含标题的数据绑定的gridview和书籍封面。封面书是在本地包中的路径名称:"files / kurikulum
2006 / [用户在上一页选择的文件夹的名称] /cover "。我无法显示书的封面,以便不显示书籍封面,错误消息如下所示:
但是标题可以显示在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 = QUOT; 0">
< GridView.ItemTemplate>
< DataTemplate>
< Grid Height =" 315"宽度= QUOT; 200"余量= QUOT; 5,10,0,0"背景= QUOT;白色">
< Grid.ColumnDefinitions>
< ColumnDefinition Width =" Auto" />
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
< RowDefinition Height =" auto" />
< RowDefinition Height =" *" />
< /Grid.RowDefinitions>
< Image Grid.Row =" 0"余量= QUOT; 10,10,10,10"高度= QUOT; 230"宽度= QUOT; 180" Source =" {Binding Image}" AutomationProperties.Name =" {Binding Name}" />
< Grid Grid.Row =" 1"余量= QUOT; 0,0,10,10"的HorizontalAlignment = QUOT;左"高度= QUOT; 50" >
< ScrollViewer Margin =" 10,10,5,5" VerticalAlignment = QUOT;陀螺"的HorizontalAlignment = QUOT;左"高度= QUOT; 40" HorizontalScrollBarVisibility = QUOT;自动" VerticalScrollBarVisibility = QUOT;禁用">
< TextBlock Margin =" 0,0,10,10" Text =" {Binding Name}"前景= QUOT;黑色"字号= QUOT; 25" VerticalAlignment = QUOT;中心"的HorizontalAlignment = QUOT;左" FontFamily =" Segoe UI Black" fontWeight设置= QUOT; SemiBold" TextAlignment = QUOT;中心" TextWrapping = QUOT;包覆与QUOT;高度= QUOT; 40"宽度= QUOT;汽车" />
< / 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(); //返回List< StorageFile>
foreach(_files中的StorageFile文件)
{
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;
Book class:
public class Book
{
public string姓名{get;组; }
public string Direktori {get;组; }
public ImageSource Image {get;组; }
}
如何处理它?</ p>
注意:
direktori.Direktori = kurikulum 2006(或其他文件夹)
direktori.Name =用户在上一页选择的文件夹名称
嗨Priscillia,
我已经尝试过您的代码,没有问题。
基于FileNotFoundException,我建议你查看你的代码并确保以下几点:
< li style ="text-autospace:none">这些文件是否存在于项目的文件夹中?- 当你使用"direktori"变量时是否有值选择了上一页的文件夹?
- 那些是jpg格式的图片吗?我的意思是它可能是png和jpeg格式,你可以改变代码
thumbFile = await _folder.GetFileAsync(file.DisplayName.ToString()+" .jpg");
到
到thumbFile = await _folder.GetFileAsync(file.Name);
最好的问候
I have a gridview with data bindings that contain titles and book covers. Cover the book is in the local package with the name of the path: "files/kurikulum 2006/[The name of the folder selected by the user on the previous page]/cover".I have trouble displaying the cover of the book so that the book cover does not appear and the error message appears as below:
But for the title can be displayed on the 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>
Code:
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;
Book class:
public class Book { public string Name { get; set; } public string Direktori { get; set; } public ImageSource Image { get; set; } }
How to handle it?
Note:
direktori.Direktori = kurikulum 2006 (or the other folder)
direktori.Name = The name of the folder selected by the user on the previous page
Hi Priscillia,
I have tried your code and there is no problem.
Based on the FileNotFoundException, I suggest you check your code and make sure the following several points:
- Are those files exist in the folder of your project?
- Whether The variable of "direktori" have a value when you selected the folder on the previous page?
- Are those all pictures in jpg format? I mean it may be in png and jpeg format,you can change the code
thumbFile = await _folder.GetFileAsync(file.DisplayName.ToString() + ".jpg");
to
to thumbFile = await _folder.GetFileAsync(file.Name);
Best regards
这篇关于[UWP] [C#]无法在本地包的gridview数据绑定上显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!