以xamarin格式将base64string绑定到listview

首先我是Xamarin.Form的新手。 我试图从Google获得最好的效果,但是一些function甚至无法search到。

我正在创build一个Xamarin.Form应用程序。 在那个应用程序中,我正在将图像存储到sql server base64 string格式,而我在sql server中的数据types是varchar(Max)

我的问题是,如何将base64 string转换为图像,并与图像绑定到列表视图。

代码清单:

 <ListView x:Name="listView" HasUnevenRows="true" SeparatorColor="Gray"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <Image Source="{Binding image}" 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> 

c#代码:

 Public async Task loadDeveloperList() { try { List<employee> employeeDetail = new List<employee>(); HttpClient client = new HttpClient(); StringBuilder sb = new StringBuilder(); client.MaxResponseContentBufferSize = 256000; var RestUrl = "http://example.com/Getemployee/"; var uri = new Uri(RestUrl); actIndicator.IsVisible = true; actIndicator.IsRunning = true; var response = await client.GetAsync(uri); if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); List<employee> onjEmployee = JsonConvert.DeserializeObject<List<employee>>(content); foreach (var item in onjEmployee) { employee emptemp = new employee() { empID = item.empID, name = item.name, city = item.city, post = item.post, salary = item.salary, gender = item.gender, image = item.image }; string cFotoBase64 = emptemp.image; byte[] ImageFotoBase64 = System.Convert.FromBase64String(cFotoBase64); employeeDetail.Add(emptemp); } listView.ItemsSource = employeeDetail; } } catch (Exception ex) { } } 

所以任何一个请给我一个想法和任何解决scheme。

根据这个论坛主题,你可以为它创建一个转换器。 只要将Base64字符串保留为Employee一部分即ie Base64Image属性。

现在定义一个这样的转换器。

 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(); } } 

现在在你的XAML中声明并使用如下的转换器:

添加命名空间到页面根,我将假设它是一个普通的ContentPage ,所以它应该看起来像这样:

 <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"> 

请注意, YourAppYourPage等应该替换为您的实际应用程序名称和正确的名称空间。

现在将转换器声明为页面的一部分。

 <ContentPage.Resources> <ResourceDictionary> <local:ConverterBase64ImageSource x:Key="Base64ToImageConverter" /> </ResourceDictionary> </ContentPage.Resources> 

最后在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> 

现在应该显示你的图片!