Xamarin.Forms - 在listview模板中显示嵌入式资源图像

时间:2018-01-28 03:42:48

标签: xamarin.forms xamarin.forms.listview

我在Xamarin.Forms中有一个列表视图,我已经设置了ItemTemplate,例如

listView.ItemTemplate = new DataTemplate(() =>
{
  Image img = new Image();
  img.SetBinding(Image.SourceProperty, "ImageUrl");
  ...so on
}

如果图像存储在同一个项目中但我已将图像移动到PCL项目并设置为"嵌入式资源"

我需要说明下面的内容,但它不起作用,我怎样才能实现这一点,因此它将绑定"嵌入式资源"在列表项中进行图像控制。

img.SetBinding(Image.SourceProperty, "ImageSource.FromResource(ImageUrl)");

由于

2 个答案:

答案 0 :(得分:0)

指定图像存在的程序集

ImageSource.FromResource("yourNamespace.imageName.png", typeof(className).GetTypeInfo().Assembly))

答案 1 :(得分:0)

在模型中创建了readonly属性以返回ImageSource,它仍然使用ImageUrl属性返回ImageSource,现在我只是绑定到这个新的readonly属性。

//in model    
[JsonIgnore]
public ImageSource ImageUrlSource { get { return ImageSource.FromResource(ImageUrl); } }



listView.ItemTemplate = new DataTemplate(() =>
{
Image img = new Image();
img.SetBinding(Image.SourceProperty, "ImageUrlSource"); 
...so on
}