Xamarin Forms MVVM : Essentials

Gert Cloete 1 Reputation point
2021-04-14T06:45:40.7+00:00

Hi, I am trying to take a picture via android camera in my viewmodel and then update the Image control on the xaml page.

How do I get the picture from the ViewModel refreshed in my xaml page? The camera plugin take the image and then return to my application, but the photo on my screen never show?

public FileUploadCaptureViewModel([NotNull] INavigationService navigationService) : base(navigationService)
        {


TakePhotoCommand = new Command(async () =>
            {
                if (Xamarin.Essentials.MediaPicker.IsCaptureSupported)
                {
                    await Task.Run(async () =>
                    {
                        var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                        {
                            Directory = "",
                            SaveToAlbum = true,
                            CompressionQuality = 75,
                            CustomPhotoSize = 50,
                            PhotoSize = PhotoSize.MaxWidthHeight,
                            MaxWidthHeight = 2000,
                            DefaultCamera = CameraDevice.Front
                        });

                        if (file == null)
                            return;

                        UploadPhoto = ImageSource.FromStream(() =>
                        {
                            var stream = file.GetStream();
                            file.Dispose();
                            return stream;
                        });


                    });
 }

        public ImageSource UploadPhoto
        {
            get
            {
                return _UploadPhoto;

            }
            set
            {
                _UploadPhoto = value;
                RaisePropertyChanged("UploadPhoto");
            }
        }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-04-14T09:11:15.123+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Did you try to debug on the value UploadPhoto to see if it has value or not ?

    Try to set Directory and Name in StoreCameraMediaOptions to make sure that the image is saved into album successfully.

       var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions  
           {  
               Directory = "Sample",  
               Name = "test.jpg"  
           });  
    

    Check the usage here .


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments