Une façon de le faire est d'afficher une ActionSheet avec Prism.IPageDialogService
pour donner des choix à l'utilisateur. Vous pouvez même faire quelque chose comme ça d'avoir une solution multi-plateforme:
private async void AddImage() // This is your method for your button
{
string chosen = await _dialogService.DisplayActionSheetAsync("Which source do you want to use?", "Cancel", null, "Camera", "Galery");
if (chosen == "Camera")
{
TakePhoto();
}
else if (chosen == "Galery")
{
PickPhoto();
}
}
Mise en œuvre de TakePhoto()
:
private async void TakePhoto()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await _dialogService.DisplayAlertAsync("Warning", "Camera not available", "OK");
return;
}
_mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
SaveToAlbum = true,
Directory = "Sample",
Name = "sample" + DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmss") + ".jpg"
});
if (_mediaFile == null)
{
return;
}
ImageButtonAddGroup = _mediaFile.Path;
}
Mise en œuvre de PickPhoto()
:
private async void PickPhoto()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await _dialogService.DisplayAlertAsync("Warning", "Camera not available", "OK");
return;
}
_mediaFile = await CrossMedia.Current.PickPhotoAsync();
if (_mediaFile == null)
{
return;
}
ImageButtonAddGroup = _mediaFile.Path;
}
Si vous voulez deux boutons au lieu d'un, vous n'avez pas besoin de Prism.IPageDialogService
et vous pouvez simplement lier les méthodes TakePhoto()
et PickPhoto()
à deux différents boutons.