我正在尝试在我的UWP Xamarin项目中显示图像列表。
我不想使用ImageCell。
这是Xamarin论坛的示例代码。
但我无法完成此代码以便成功运行。
这是我的代码。
<ListView x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="#eee"
Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Image Source="{Binding image}" />
<Label Text="{Binding title}"
TextColor="#f35e20" />
<Label Text="{Binding subtitle}"
HorizontalOptions="EndAndExpand"
TextColor="#503026" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
public class ImageItem {
string title;
ImageSource image;
string subtitle;
}
ImageItem a= new ImageItem();
a.title = "XXX";
a.image = ImageSource.FromFile(String.Format("{0}{1}.png", Device.OnPlatform("Icons/", "", "Assets/"), "noimage"));
a.subtitle = "XXX";
list.Add(a);
listview.itemsSource = list;
我在UWP Xamarin Project的Assets文件夹中有noimage.png。
我该怎么办?
答案 0 :(得分:0)
如果您想使用绑定,我相信您需要ImageItem
来实现INotifyPropertyChanged
接口。您可以尝试设置类ImageItem,如下所示:
public class ImageItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
string _title;
public string title
{
get
{
return _title;
}
set
{
if (_title != value)
_title = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("title"));
}
}
}
ImageSource _image;
public string image
{
get
{
return _image;
}
set
{
if (_image != value)
_image = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("image"));
}
}
}
string _subtitle;
public string subtitle
{
get
{
return _subtitle;
}
set
{
if (_subtitle != value)
_subtitle = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("subtitle"));
}
}
}
}