我ListView
在XAML中有一个,并List<string>
拥有本地嵌入式图像路径。我无法在列表中显示图像。顺便说一句,我可以通过
<Image Source="{local:ImageResource TypingApplication.Images.Icons.Search.png}" />
但是我无法显示中的图像ListView
。这是我的XAML代码
<ListView x:Name="ListView"
ItemsSource="{Binding ListItems}"
IsEnabled="True"
IsVisible="True"
RowHeight="40"
Opacity="0.9">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Image Source="{local:ImageResource TypingApplication.Images.Icons.{Binding .}}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我已经ImageResourceExtension
在Extensions
文件夹和xmlns:local="clr-namespace:TypingApplication.Extensions"
XAML中添加了内容,正如我提到的那样,我可以显示“单个图像”,仅存在问题ListView
。
这是我的C#代码,其中包含List和Construcor
public List<string> ListItems
{
get
{
return new List<string>()
{
"Home.png",
"Favorite.png",
"Search.png"
};
}
}
public HomePage()
{
InitializeComponent();
this.BindingContext = this;
}
请注意,我在项目中使用共享图像。我已将所有图像的属性设置为Embedded resource
中SolutionExplorer
。
ObservableCollection
IValueConverter
实现将绑定转换为所需值EmbeddedResource
public class EmbeddedToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string fileName && parameter is String assemblyName)
{
try
{
var imageSource = ImageSource.FromResource(assemblyName + "." + fileName, typeof(EmbeddedToImageSourceConverter).GetTypeInfo().Assembly);
return imageSource;
}
catch (Exception)
{
return value;
}
}
else
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
XAML
<ContentPage.Resources>
<local:EmbeddedToImageSourceConverter x:Key="converter"/>
</ContentPage.Resources>
在列表视图中,添加刚创建的绑定wrto转换器资源。
<Image Source="{Binding ., Converter={StaticResource converter}, ConverterParameter='TypingApplication.Images.Icons'}"/>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句