Search code examples
cameraxamarin.iosxamarin.androidxamarin.formsxamarin-studio

Xamarin camera not on main navigation page


I've managed to get the camera going cross platform using xamarin and this tutorial:

Camera access with Xamarin.Forms

I'm now trying to get it working on a different navigation form (The camera functionality would be several forms away from the main page.) However the device specific code accesses many things wired up to the App instance which I'm struggling to wire up from another form. Does anyone know of a good camera example that isn't on the main page? I've been coding C# for years but I'm new to Xamarin and the camera stuff seems to be the hardest to get going. Thanks in advance.

Jeff


Solution

  • use the Media plugin

    takePhoto.Clicked += async (sender, args) =>
    {
        await CrossMedia.Current.Initialize();
    
        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            DisplayAlert("No Camera", ":( No camera available.", "OK");
            return;
        }
    
        var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            Directory = "Sample",
            Name = "test.jpg"
        });
    
        if (file == null)
            return;
    
        await DisplayAlert("File Location", file.Path, "OK");
    
        image.Source = ImageSource.FromStream(() =>
        {
            var stream = file.GetStream();
            file.Dispose();
            return stream;
        }); 
    };