Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to display Media Picker when a user navigates to a tabbed page

ikreieikreie Member ✭✭
edited August 2020 in Xamarin.Forms

I'm trying to display James Montemagno's Media Picker immediately when a user navigates to one of my tabbed pages. I found a function called OnAppearing() that I tried overriding to create this result. Although it technically shows the camera immediately when I switch tabs, after I close out of the media picker I get an error saying "only one operation can be active at a time".

Here is how I'm trying to implement this feature:

    protected override async void OnAppearing()
        {
            TakePhotoButton_Clicked();
        }


        async void TakePhotoButton_Clicked()
        {
            //Allows users to take pictures in the app
            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                DisplayAlert("No Camera", "Camera is not available.", "OK");
                return;
            }

            var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                //Sets the properties of the photo file 
                SaveToAlbum = true,
                PhotoSize = PhotoSize.MaxWidthHeight,
                DefaultCamera = CameraDevice.Rear
            });

            if (file == null)
                return;
        }

I'm pretty new to all of this and I feel as if I'm making a technical error. I read this post https: //damian.fyi/2016/07/06/only-one-operation-can-be-active-at-at-time/ about someone getting the same error, but I'm not catching how I could be achieving this feature differently.

(remove the space between https: and the // because this forum wouldn't allow me to post links

Best Answers

Answers

  • ikreieikreie Member ✭✭

    @LeonLu thanks. This gets rid of the error, but it only displays the camera the first time I click on the tab. If I switch back and forth between tabs, the camera will not longer be displayed, its just a white screen. How can I make it display every time I hit the tab?

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Do you want to achieve the result like following GIF?

    If so, you can reset the _loaded tag like following code.

        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class Page1 : ContentPage
        {
            public Page1 ()
            {
                InitializeComponent ();
            }
    
            bool _loaded = false;
            protected override void OnAppearing()
            {
                base.OnAppearing();
                TakePhotoButton_Clicked();
            }
    
            async void TakePhotoButton_Clicked()
            {
                //Allows users to take pictures in the app
                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    DisplayAlert("No Camera", "Camera is not available.", "OK");
                    return;
                }
                if (_loaded) return;
                _loaded = true;
                var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
    
                    //Sets the properties of the photo file 
                    SaveToAlbum = true,
                    PhotoSize = PhotoSize.MaxWidthHeight,
                    DefaultCamera = CameraDevice.Rear
                });
                _loaded = false;
    
                myImage.Source = ImageSource.FromStream(() =>
                {
                    var stream = file.GetStream();
                    return stream;
                });
                if (file == null)
                    return;
            }
        }
    }
    
    
Sign In or Register to comment.