1

hello I am working on a app that allows users to take pictures of notes and then send them to a friend. I am using xamarin forms do build this app and I am also using the media plugin to access the native camera but the user has to press a button to open the native camera so My question is how do I get the camera to load as soon as the user open's the app?

heres my xaml code:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SnapNote.CameraPage"
xmlns:local="clr-namespace:AppName;"
    BackgroundColor="{x:Static local:Colors.BackgroundColor}">
    <ContentPage.Padding>
        <OnPlatform
            x:TypeArguments="Thickness"
            iOS="10,20,10,10"
            Android="10,10,10,10" />
    </ContentPage.Padding>
    <StackLayout>

        <Image Source="TakePhotoButton.png">
    <Image.GestureRecognizers>
        <TapGestureRecognizer
                Tapped="Handle_Clicked"  />
  </Image.GestureRecognizers>
</Image>

        <Image x:Name="image"/>


        <Image Source="SendNoteButton.png">

</Image>


    </StackLayout>
</ContentPage> 

and heres the code behind:

using System;
using System.Collections.Generic;
using Plugin.Media;
using Plugin.Media.Abstractions;
using Xamarin.Forms;

namespace AppName
{
    public partial class CameraPage : ContentPage
    {
        public CameraPage()
        {
            InitializeComponent();
        }

        async void Handle_Clicked(object sender, System.EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
            {
                Directory = "MyPhoto",
                Name = "Nextflow.jpg",
                SaveToAlbum = true
            });

            if (file == null)
                return;

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

any help would be amazing!

Thanks in advance!

Phoneswapshop
  • 1,367
  • 8
  • 24
  • 47

1 Answers1

1

Just move your camera logic into the page's OnAppearing event

      public override void OnAppearing() {
        await CrossMedia.Current.Initialize();

        var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
        {
            Directory = "MyPhoto",
            Name = "Nextflow.jpg",
            SaveToAlbum = true
        });

        if (file == null)
            return;

        image.Source = ImageSource.FromStream(() =>
        {
            var stream = file.GetStream();
            file.Dispose();
            return stream;
        });
      }
Jason
  • 86,222
  • 15
  • 131
  • 146
  • Thank you for the reply when I tried your code suggestion I get this error: CameraPage.xaml.cs(30,30): Error CS0507: `AppName.CameraPage.OnAppearing()': cannot change access modifiers when overriding `protected' inherited member `Xamarin.Forms.Page.OnAppearing()' (CS0507) (AppName) any other suggestions? Thanks in advance :) – Phoneswapshop Mar 13 '17 at 17:55
  • Hi Jason, do you have any idea for the following question which I give bounty (+100) http://stackoverflow.com/questions/42728757/markerclick-works-but-infowindowclick-does-not-open-viewmodel – casillas Mar 13 '17 at 20:04
  • please don't hijack other people's questions with irrelevant comments – Jason Mar 13 '17 at 20:06