【问题标题】:Xamarin.Forms - Show Embedded Resource image in listview TemplateXamarin.Forms - 在列表视图模板中显示嵌入式资源图像
【发布时间】:2018-01-31 09:25:29
【问题描述】:

我在 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)");

谢谢

【问题讨论】:

    标签: xamarin.forms xamarin.forms.listview


    【解决方案1】:

    指定图像存在的程序集

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

    【讨论】:

      【解决方案2】:

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

      //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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-11
        • 1970-01-01
        • 2015-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多