问题描述
我的问题归结为为什么我的可观察集合绑定功能不起作用?我正在尝试将图像URL绑定到ListView,但是绑定将不起作用,几乎只能制作产品图像列表。如果仅执行堆栈布局并根据列表位置声明图像(即image.source = list [i] .url),就可以生成图像。
下面是xaml
<ListView ItemsSource="{Binding Actimage}">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Image Source="{Binding ImageUrl}"/>
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
下面是xaml后面的c,其中“ PicListPull”是模型,图片是先前生成的图像URL列表。
private ObservableCollection<PicListPull> actimage;
public ObservableCollection<PicListPull> Actimage;
Actimage = new ObservableCollection<PicListPull>();
for (int i = 0; i < pictures.Count(); i++)
{
var pics = new PicListPull()
{
ImageUrl = ImageSource.FromUri(pictures[i].Url)
};
Actimage.Add(pics);
}
如果我在索引处查看Actimage,它具有图像源的值,因此我无法弄清楚为什么它不显示图像。
谢谢。
更新
下面是PicListPull
public class PicListPull:INotifyPropertyChanged
{
private ImageSource imageurl;
//#region public properties
public ImageSource ImageUrl
{
get { return imageurl; }
set
{
this.imageurl = value;
RaisePropertyChanged("ImageUrl");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String name)
{
if (PropertyChanged != null)
this.PropertyChanged(this,new PropertyChangedEventArgs(name));
}
}
解决方法
ListView的单元格应为以下类型之一: TextCell,ImageCell,SwitchCell,EntryCell或CustomCell(ViewCell)
在这种情况下,如果您要使用自定义单元格,请进行如下更新:
<ListView ItemsSource="{Binding ActImage}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Image Source="{Binding ImageUrl}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
有关更多详细信息,您可以在此处查看:Custom Cells
,您需要解决三个问题
首先,您只能绑定到公共属性
ItemsSource="{Binding ActImage}"
要使其正常工作,ActImage
必须是公共财产
public ObservableCollection<PicListPull> ActImage { get; set; }
第二秒,您需要在模板中使用Cell
(如@Qwerty所述)
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Image Source="{Binding ImageUrl}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
最后,在绑定Image
时,您可以只使用包含url的字符串