【发布时间】:2020-11-20 22:14:48
【问题描述】:
我知道有人就这个话题提出了几个问题,但我都没有找到解决方案。
“FileImageInfo”模型中的字段除了图片源之外都绑定好了。
我已经按照这方面的几个教程,我看不出我在做什么不同..
//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>
我已将问题缩小到本节。如果我在图像数组中硬编码一个值,例如 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;
更新 - 正在发生,因为循环发生了一些奇怪的事情,我认为它与匿名方法有关
For example: if the for loop is: for(int i = 0; i < 1; i++)
那么你会期望项目列表只包含一项 - 图片[0] 但奇怪的是它包含的项目是图像[1]。 就像 'i' 正在递增,然后在匿名方法中传递给 images[]
【问题讨论】:
-
您不必对 url 上的图像使用 ImageSource,Forms 有一个隐式转换器设置,可以使用简单的字符串。据我所知,上面代码中的问题是 imageSource 是一个字段。您必须绑定到属性。如果将其更改为公共字符串属性并将 url 作为字符串返回,它应该可以工作。
-
@BenReierson 感谢您的输入。即使我将 ImageSource 设为属性而不是字段,它仍然不起作用。我还将该属性设为字符串并在分配上调用 ToString 但没有工作
标签: xaml listview xamarin.forms binding