2016-09-03 5 views
2

まずはXamarin.Formの新機能です。私はGoogleからベストを得ようと努力していますが、私が得ることができない機能のいくつかはたくさん検索さえしています。base64文字列をxamarin形式のリストビューにバインドします

私はXamarin.Formアプリケーションを作成しています。そのアプリでは、sql serverbase64 string形式の画像を保存しています.SQLサーバーのデータ型はvarchar(Max)です。

私の問題は、base64 stringをイメージに変換する方法とイメージをリストビューにバインドする方法です。リストビューの

コード:C#のの

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

コード:

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) 
    { 

    }   
} 

ので、いずれかが私にアイデアや任意のソリューションを提案してください。

+0

あなたの従業員クラスを投稿に入れてください –

答えて

4

thisフォーラムスレッドでは、コンバータを作成することができます。 Employeeの一部、つまりBase64ImageプロパティのBase64文字列をそのまま使用してください。

このようなコンバータを定義します。

あなたのXAMLの宣言で今すぐ
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(); 
    } 
} 

と、このようにコンバータを使用:

ページのルートに名前空間を追加し、私はそれが通常の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にコンバータを使用。

画像が表示されるはずです。

+0

Xamarinフォームで画像をエンコードしてデコードする方法について聞かれますか?ありがとうございました。 –

+0

@Cheah _from_を変換するコードは既に上記のコードにあります。 _to_ Base64を変換するコードは 'System.Convert.ToBase64String()'です。 –

+0

上記の解決策に問題があります。私のXAMLコードにConverterの定義を含めると、実際にコンバータを使用していない場合でもアプリケーションがハングします。ちょうど定義そのものが問題のようです。私はルートContentPageの直後に定義を追加しました。私は間違いなく、「Appは私のAndroid携帯電話で応答していません」。定義を削除すると、アプリはハングしません。 –

関連する問題