2017-01-13 10 views
0

このコードスニペットを見てみましょう: https://developer.xamarin.com/recipes/ios/media/video_and_photos/choose_a_photo_from_the_gallery/残念ながら、これはギャラリーをすぐに読み込んでいます。最初に選択します。 - 写真 2を取る - これらの両方が、クリックしたボタンになり、ギャラリーボタンをクリックしたときにギャラリーから画像を選択する方法(Xamarin iOS)

から既存の写真を選択

1:

は、私は、ユーザーに2つのオプションが提供しようとしています。何か案は?

答えて

0

これを行う1つの方法は、Prism.IPageDialogServiceのActionSheetを表示してユーザーに選択肢を与えることです。あなたも、クロスプラットフォームソリューションを持っているためにこのような何かを行うことができます。PickPhoto()

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; 
} 

実装:TakePhoto()

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(); 
    } 
} 

実装

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; 
} 

をあなたの代わりに2つのボタンが必要な場合1つのうち、Prism.IPageDialogServiceは不要で、メソッドTakePhoto()PickPhoto()を2つのdにバインドすることができます異なるボタン。

関連する問題