Last active
November 21, 2019 02:10
-
-
Save icebeam7/937fd0b632f02b2d07396c0a7b0d8fe6 to your computer and use it in GitHub Desktop.
DemoCamara: PhotoViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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