Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Last active November 21, 2019 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebeam7/937fd0b632f02b2d07396c0a7b0d8fe6 to your computer and use it in GitHub Desktop.
Save icebeam7/937fd0b632f02b2d07396c0a7b0d8fe6 to your computer and use it in GitHub Desktop.
DemoCamara: PhotoViewModel.cs
using System.Windows.Input;
using System.Threading.Tasks;
using Xamarin.Forms;
using DemoCamara.Services;
namespace DemoCamara.ViewModels
{
public class PhotoViewModel : BaseViewModel
{
CameraService cameraService;
private ImageSource _photo;
public ImageSource Photo
{
get { return _photo; }
set { _photo = value; OnPropertyChanged(); }
}
public ICommand TakePhotoCommand { get; private set; }
public ICommand ChoosePhotoCommand { get; private set; }
public PhotoViewModel()
{
cameraService = new CameraService();
Task.Run(async () => await cameraService.Init());
TakePhotoCommand = new Command(async () => await TakePhoto());
ChoosePhotoCommand = new Command(async () => await ChoosePhoto());
}
private async Task TakePhoto()
{
var file = await cameraService.TakePhoto();
if (file != null)
Photo = ImageSource.FromStream(() => file.GetStream());
}
private async Task ChoosePhoto()
{
var file = await cameraService.ChoosePhoto();
if (file != null)
Photo = ImageSource.FromStream(() => file.GetStream());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment