以xamarin的形式绑定base64字符串到listview

本文关键字:base64 字符串 listview 绑定 xamarin | 更新日期: 2023-09-27 18:05:23

首先,我是Xamarin.Form的新手。我正试图从谷歌得到最好的,但一些功能,我不能得到甚至搜索很多。

我正在创建Xamarin。表单应用程序。在该应用程序中,我将图像存储到sql server中的base64 string格式和我在sql server中的数据类型是varchar(Max)

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

Listview代码:
<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)
    {
    }          
}

请大家给我提个建议和解决方案。

以xamarin的形式绑定base64字符串到listview

根据这个论坛的帖子,你可以为它创建一个转换器。只需将Base64字符串作为Employee的一部分,即在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>

您的图像现在应该显示!