[英]Can't bind images to ListView in Xamarin Forms
I know several questions have been asked on this topic but I've not found a solution in any of them.我知道有人就这个话题提出了几个问题,但我都没有找到解决方案。
The fields in 'FileImageInfo' model all bind ok except the image source. 'FileImageInfo' model 中的字段除图像源外均绑定正常。
I've followed several tutorials on this and I can't see what I'm doing differently..我已经按照这方面的几个教程,我看不出我在做什么不同..
//code behind
var items = new List<FileImageInfo>();
for (int i = 0; i < images.Count; i++)
{
items.Add(new FileImageInfo
{
FileType = "Jpeg",
FileSize = 3.4,
DateCreated = DateTime.Now,
imageSource = "http://lorempixel.com/100/100/people/1"
//ImageSource.FromStream(() => images[i].AsPNG().AsStream())
});
}
Items.ItemsSource = items;
//model
public class FileImageInfo
{
public string FileType { get; set; }
public double FileSize { get; set; }
public DateTime DateCreated { get; set; }
//public ImageSource imageSource { get; set; }
public string imageSource;
public FileImageInfo()
{
}
}
//Xaml
<StackLayout >
<ListView BackgroundColor="White" x:Name="Items" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="5" Orientation="Horizontal">
<StackLayout HorizontalOptions="StartAndExpand">
<StackLayout Orientation="Horizontal">
<Label Text="{Binding FileType}"></Label>
<Label></Label>
<Label Text="{Binding FileSize}"></Label>
</StackLayout>
<Label Text="{Binding DateCreated}"></Label>
<Image Source="{Binding imageSource}" />
</StackLayout>
<Label Text="Status" VerticalOptions="Center"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
I've narrowed the issue down to this section.我已将问题缩小到本节。 If I hard code a value in the images array such as images[0] it works, but when I use the iterator variable images[i] nothing is displayed
如果我在图像数组中硬编码一个值,例如 images[0] 它可以工作,但是当我使用迭代器变量 images[i] 时,什么都不会显示
for (int i = 0; i < images.Count; i++)
{
items.Add(new FileImageInfo
{
FileType = "Jpeg",
FileSize = 3.4,
DateCreated = DateTime.Now,
imageSource = ImageSource.FromStream(() =>
images[i].AsPNG().AsStream())
});
}
Items.ItemsSource = items;
Update - There is happening because there's something strange happening with the loop and I think it has something to do with the anonymous method更新-发生了,因为循环发生了一些奇怪的事情,我认为这与匿名方法有关
For example: if the for loop is: for(int i = 0; i < 1; i++)
then then you would expect the items list to only contain one item - images[0] but weirdly the item it contains is images[1].那么你会期望项目列表只包含一项 - 图像 [0] 但奇怪的是它包含的项目是图像 [1]。 It's like 'i' is getting incremented and THEN passed into images[] in the anonymous method
就像'i'正在增加,然后在匿名方法中传递给 images[]
Froms shared code of FileImageInfo
, modify type of imageSource
from string
to ImageSource
as follow:从 FileImageInfo 的共享代码中,将
FileImageInfo
的imageSource
从string
修改为ImageSource
如下:
public class FileImageInfo
{
public string FileType { get; set; }
public double FileSize { get; set; }
public DateTime DateCreated { get; set; }
public ImageSource imageSource { get; set; }
//public string imageSource { set; get; }
public FileImageInfo()
{
}
}
Then adding ItemSource
for ListView :然后为ListView添加
ItemSource
:
for (int i = 0; i < 5; i++)
{
items.Add(new FileImageInfo
{
FileType = "Jpeg",
FileSize = 3.4,
DateCreated = DateTime.Now,
imageSource = ImageSource.FromUri(new Uri("https://s1.ax1x.com/2020/07/31/aQhvss.png"))
}) ;
}
Items.ItemsSource = items;
Here I replace shared uri to mine, becasue shared uri is invalid in Image.这里我将共享 uri 替换为我的,因为共享 uri 在 Image 中无效。 And it will not show image as result.
并且它不会显示图像作为结果。 Suggest that using https uri.The error log as follow:
建议使用https uri。错误日志如下:
ImageLoaderSourceHandler: Could not retrieve image or image data was invalid: Uri: http://lorempixel.com/100/100/people/1/
ImageLoaderSourceHandler:无法检索图像或图像数据无效:Uri: http://lorempixel.com/100/100/people/1/
Now the image will show:现在图像将显示:
Note: If must need to use http uri, you can use FFImageLoading Nuget Pagckage to show image.Then FileImageInfo class can use sting
type for imageSource
.注意:如果必须使用http uri,可以使用FFImageLoading Nuget 包
imageSource
显示图像。然后 FileImageInfo sting
图像可以使用类型。
public string imageSource { set; get; }
Then add data:然后添加数据:
for (int i = 0; i < 5; i++)
{
items.Add(new FileImageInfo
{
FileType = "Jpeg",
FileSize = 3.4,
DateCreated = DateTime.Now,
imageSource = "https://s1.ax1x.com/2020/07/31/aQhvss.png"
}) ;
}
Used in Xaml:用于 Xaml:
xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
<ffimageloading:CachedImage HorizontalOptions="Center"
VerticalOptions="Center"
DownsampleToViewSize="true"
Source="{Binding imageSource}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.