XAML bound Image not displaying

TripVoltageTripVoltage GBMember
edited October 2016 in Xamarin.Forms

Hi I am trying to display an image taken from camera using cross media plugin:

Here is the XAML:
<StackLayout Grid.Row="0" Grid.Column="1" Padding="20" Spacing="20"> <Button x:Name="takePhoto" WidthRequest="100" HeightRequest="80" BackgroundColor="White" Text="Take photo" Command="{Binding LaunchTakePhotoAction}"/> <Button x:Name="choosePhoto" WidthRequest="100" HeightRequest="80" BackgroundColor="White" Text="Choose photo" Command="{Binding LaunchUploadPhotoAction}"/> <Button x:Name="chooseDocument" WidthRequest="100" HeightRequest="80" BackgroundColor="White" Text="Choose document"/> <Image Source="{Binding GetImageSource}" Aspect="AspectFit"/> <Button Image="{Binding GetImageSource}"/> </StackLayout>

I tried to display the image as both Button and Image elements with no luck.

Here is code from ViewModel to get image:
public Image image; public ImageSource GetImageSource { get { return image; } set { if (image != value) { image = value; RaisePropertyChanged(); } } }

Here is code from ViewModel to take photo and set image data:
` async void LanchTakePhotoCommand()
{

        string name = ("Image_" + DateTime.Now.ToString("yyMMddhhmmss") + ".JPG");

        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            await Application.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "OK");
            return;
        }

        var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
        {
            Directory = "Photos",
            Name = name,
            //PhotoSize = PhotoSize.Medium, // This does not work in debug on Android only release mode!!
            SaveToAlbum = true
        });

        if (file == null)
            return;

        await Application.Current.MainPage.DisplayAlert("File Location", file.AlbumPath, "OK");

        image = ImageSource.FromStream(() =>
        {
            Stream stream = file.GetStream();
            file.Dispose();
            return stream;
        });
    }`

I'm using Android emulator to test but nothing is displaying in IU? I can take photo and save to storage but when I try to display in UI nothing shows.

Answers

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    My guess is you dispose the file but expect that the stream is still valid.

  • TripVoltageTripVoltage GBMember

    @ThomasBurkhart Thanks I tried that also. Turns out it was simple mistake of caling not RaisePropertyChanged() as I should have been calling OnPropertyChanged() to update the UI...!

Sign In or Register to comment.