As per this forum thread you could create a converter for it.
Just keep the Base64 string as part of your Employee
i.e. in the Base64Image
property.
Now define a converter like this.
public class ConverterBase64ImageSource : IValueConverter
{
public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
{
string base64Image = (string)value;
if (base64Image == null)
return null;
// Convert base64Image from string to byte-array
var imageBytes = System.Convert.FromBase64String(base64Image);
// Return a new ImageSource
return ImageSource.FromStream (() => {return new MemoryStream(imageBytes);});
}
public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
{
// Not implemented as we do not convert back
throw new NotSupportedException();
}
}
Now in your XAML declare and use the converter like this:
Add the namespace to the page root, I'm going to assume it's a normal ContentPage
, so it should look something like:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:YourApp;assembly=YourApp"
x:Class="YourApp.YourPage">
Note that YourApp
and YourPage
, etc. should be replaced with your actual app name and the right namespaces.
Now declare the converter as part of your page.
<ContentPage.Resources>
<ResourceDictionary>
<local:ConverterBase64ImageSource x:Key="Base64ToImageConverter" />
</ResourceDictionary>
</ContentPage.Resources>
Finally use the converter on the Image
.
<ListView x:Name="listView" HasUnevenRows="true" SeparatorColor="Gray">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Image Source="{Binding Base64Image, Converter={StaticResource Base64ToImageConverter}}}" Grid.Row="0"
Grid.RowSpan="3" Grid.Column="0"
HorizontalOptions="Center" HeightRequest="50"
VerticalOptions="Center">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnImageTapped" />
</Image.GestureRecognizers>
</Image>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Your images should now be shown!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…