2014-09-22 21 views

回答

1

我花的時間與ACR樣品和仍然沒有想通了這一切完全是如何結合在一起的。所以我最終從頭開始編寫App.cs文件中的所有內容,並通過試用/錯誤提出了這個非常簡單的解決方案,適用於iOS和Android。它允許您從圖庫中獲取圖像或拍攝照片,完成後會顯示照片。希望這會幫助某人節省幾個小時。

我也想向那些努力爲社區創造出色的圖書館的人提出請求:請提供超級簡單的使用示例。不要創建一個展示的一切資料庫一個精心製作的樣本...

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Diagnostics; 
using Xamarin.Forms; 
using Acr.XamForms.Mobile.Media; 

namespace photoSimpleTest 
{ 
    public class App : Application 
    { 
     MediaPicker mPick = new MediaPicker(); 

     Image ShowPic; 

     public App() 
     { 
      var showGalleryButton = new Button { 
       Text="Show Gallery" 
      }; 
      showGalleryButton.Clicked += ShowGallery; 

      var takePictureButton = new Button 
      { 
       Text = "Take Picture" 
      }; 
      takePictureButton.Clicked += TakePicture; 

      ShowPic = new Image(); 

      MainPage = new ContentPage 
      { 
       Content = new StackLayout 
       { 
        VerticalOptions = LayoutOptions.Center, 
        Children = { 
         showGalleryButton, 
         takePictureButton, 
         ShowPic 
        } 
       } 
      }; 
     } 

     public async void ShowGallery(object s, EventArgs a) 
     { 
      if (!mPick.IsPhotoGalleryAvailable) 
       Debug.WriteLine("Photo Gallery is unavailable"); 
      else 
      { 
       var result = await mPick.PickPhoto(); 
       PhotoReceived(result); 
      } 
     } 

     public async void TakePicture(object s, EventArgs a) 
     { 
      if (!mPick.IsCameraAvailable) 
       Debug.WriteLine("Camera is not available"); 
      else 
      { 
       var opt = new CameraOptions { }; 
       var result = await mPick.TakePhoto(opt); 
       PhotoReceived(result); 
      } 
     } 

     private void PhotoReceived(IMediaFile file) 
     { 
      if (file == null) 
       Debug.WriteLine("Photo Cancelled"); 
      else 
       ShowPic.Source = ImageSource.FromFile(file.Path); 
     } 
    } 
} 
相關問題