2016-05-06 41 views
5

Sono abbastanza nuovo per Xamarin e XAML cose e qui è quello che ho fatto finora nel mio progetto portatile utilizzato da Android & iPhone (solo con Android):Xamarin - Immagine da stringa Base64

Articolo cs (caricati da JSON)

[JsonProperty("image")] 
    private string ImageBase64 { get; set; } 

    [JsonIgnore] 
    private Xamarin.Forms.Image _image = null; 

    [JsonIgnore] 
    public Xamarin.Forms.Image Image 
    { 
     get 
     { 
      if (_image == null) 
      { 
       _image = new Xamarin.Forms.Image() 
       { 
        Source = Xamarin.Forms.ImageSource.FromStream(() => new MemoryStream(Convert.FromBase64String(ImageBase64))), 
        BackgroundColor = Color.White, 
        WidthRequest = 64, 
        HeightRequest = 64, 
       }; 
       OnPropertyChanged("Image"); 
      } 
      return _image; 
     } 
     private set 
     { _image = value; } 
    } 

ItemsView.xaml:

<StackLayout VerticalOptions="FillAndExpand" Padding="5,20,5,0" > 
    <Label Text="Items" VerticalOptions="Center" Font="35" HorizontalOptions="Center" /> 
    <ListView x:Name="list" ItemsSource="{Binding Items}"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
     <ImageCell 
         Text="{Binding ItemName}" 
         Detail="{Binding Infos, StringFormat='{0}'}" 
      Image.Source="{Binding Path=Image}"> 
     </ImageCell> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
    </ListView> 
</StackLayout> 

ho correttamente ottenere le mie etichette visualizzate ma l'immagine non è. Qualcuno può spiegarmi cosa sto facendo male?

+0

2 cose saltano fuori da me: a) si sta vincolando un ImageSource a un'immagine b) "Path =" non è necessario – Jason

risposta

6

Non ho mai usato Xamarin, ma ci sono alcuni problemi evidenti con il tuo codice.

Il tipo di vostra proprietà Image dovrebbe essere ImageSource, non Image, come si a quanto pare desidera associare la proprietà di un ImageCell ImageSource. Inoltre, chiamare OnPropertyChanged in un getter di proprietà non funziona mai, perché l'evento PropertyChanged deve essere attivato prima del un legame (o qualsiasi altro consumatore) può ottenere un valore di proprietà modificato.

Invece di Image.Source="{Binding ...}, l'associazione corretta avrebbe dovuto essere

<ImageCell ... ImageSource="{Binding Path=Image}" /> 

Le proprietà devono essere dichiarate in questo modo:

private string imageBase64; 
public string ImageBase64 
{ 
    get { return imageBase64; } 
    set 
    { 
     imageBase64 = value; 
     OnPropertyChanged("ImageBase64"); 

     Image = Xamarin.Forms.ImageSource.FromStream(
      () => new MemoryStream(Convert.FromBase64String(imageBase64))); 
    } 
} 

private Xamarin.Forms.ImageSource image; 
public Xamarin.Forms.ImageSource Image 
{ 
    get { return image; } 
    set 
    { 
     image = value; 
     OnPropertyChanged("Image"); 
    } 
} 

Se davvero bisogno creazione pigro del valore Image immobile , è possibile renderlo di sola lettura e effettuare la chiamata OnPropertyChanged corrispondente nel set ImageBase64:

private string imageBase64 
public string ImageBase64 
{ 
    get { return imageBase64; } 
    set 
    { 
     imageBase64 = value; 
     OnPropertyChanged("ImageBase64"); 
     OnPropertyChanged("Image"); 
    } 
} 

private Xamarin.Forms.ImageSource image; 
public Xamarin.Forms.ImageSource Image 
{ 
    get 
    { 
     if (image == null) 
     { 
      image = Xamarin.Forms.ImageSource.FromStream(
       () => new MemoryStream(Convert.FromBase64String(ImageBase64))); 
     } 
     return image; 
    } 
} 
+0

Ha funzionato, grazie :) –