2016-09-14 4 views
0

J'ai vu de nombreux exemples ici et j'essaie de m'adapter à mon code! Mais, quand l'image apparaît, elle est vide ... Je veux juste montrer l'image après le clic du bouton. Ne pas économiser sur le smartphone.Afficher une image sur Xamarin à partir de byte []

Le code .cs:

ApiCall apiCallFoto = new ApiCall(); 
ImageSource imagem = null; 

btnFoto.Clicked += async (sender, e) => 
{ 
    await apiCallFoto.GetFoto<byte[]>("Nomes", "Foto", envolvID).ContinueWith(t => 
    { 
     //Aqui verificamos se houve problema ne requisição 
     if (t.IsFaulted) 
     { 
      Debug.WriteLine(t.Exception.Message); 
      Device.BeginInvokeOnMainThread(() => 
      { 
        DisplayAlert("Falha", "Ocorreu um erro na Requisição :(", "Ok"); 
      }); 
     } 
     //Aqui verificamos se a requisição foi cancelada por algum Motivo 
     else if (t.IsCanceled) 
     { 
      Debug.WriteLine("Requisição cancelada"); 

      Device.BeginInvokeOnMainThread(() => 
      { 
        DisplayAlert("Cancela", "Requisição Cancelada :O", "Ok"); 
      }); 
     } 

     //Caso a requisição ocorra sem problemas, cairemos aqui 
     else 
     { 
      //Se Chegarmos aqui, está tudo ok, agora itemos tratar nossa Lista 
      Device.BeginInvokeOnMainThread(() => 
      { 
       byte[] fotoBytes = t.Result; 
       // Image image = new Image(); 
         imagem = ImageSource.FromStream(() => new MemoryStream(fotoBytes)); 

        Navigation.PushAsync(new FotoEnvolvido(imagem)); 

      }); 

      } 
    }); 
    }; 

Les FotoEnvolvido.cs:

public partial class FotoEnvolvido : ContentPage 
{ 
    private ImageSource imagem; 

    public FotoEnvolvido(ImageSource imagem) 
    { 
     InitializeComponent(); 
     this.imagem = imagem; 
     BindingContext = imagem; 

    } 
} 

Le FotoEnvolvido.xaml:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="100"/> 
    </Grid.RowDefinitions> 
    <Image Source="{Binding imagem}" x:Name="fotoperfil"/> 

</Grid> 

Répondre

2

Votre BindingContext doit être this ou vous devez lier à .

public partial class FotoEnvolvido : ContentPage 
{ 
    public ImageSource Imagem {get; set; } 

    public FotoEnvolvido(ImageSource imagem) 
    { 
     InitializeComponent(); 
     Imagem = imagem; 
     BindingContext = this; 
    } 
} 

<Image Source="{Binding Imagem}" x:Name="fotoperfil"/> 

ou

public partial class FotoEnvolvido : ContentPage 
{ 
    public FotoEnvolvido(ImageSource imagem) 
    { 
     InitializeComponent(); 
     BindingContext = imagem; 
    } 
} 

<Image Source="{Binding}" x:Name="fotoperfil"/> 
+0

Niice !! J'ai travaillé ... merci mon ami !! –