question

GertCloete-2320 avatar image
0 Votes"
GertCloete-2320 asked ColeXia-MSFT answered

Xamarin Forms MVVM : Essentials

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");
            }
        }
dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.