2016-12-07 33 views
0

我试图将EmbeddedImages绑定到ListView,因此我可以根据列表中的项显示不同的图标。使用Working with Images教程我已经设法做一个扩展来显示嵌入的图像,但只有当我硬编码的位置图标。在Xamarin XAML扩展中使用绑定

所以此工程:

<Image Source="{local:ImageResource (namespace).icon.png}"/> 

,但我不能绑定到我想要的图标。我试过所有这些,但都没有工作:

<Image Source="{local:ImageResource {Binding _iconLocation}}" /> 

<Image Source="{Binding Source=local:ImageResource, Path=_iconLocation}"/> 

回答

1

如果你想使用绑定,你不需要使用标记扩展。扩展完成“因为没有从字符串到ResourceImageSource的内置类型转换器”。 在模型中定义

public ImageSource MyImageSource 
     { 
      get 
      { //here you decide what resource to use 
       return ImageSource.FromResource("(namespace).icon.png"); 
      } 
     } 

然后在XAML中使用

<Image Source="{Binding MyImageSource}" /> 

而且你不需要任何扩展类