2015-06-24 184 views
-1

我已經使用下面的代碼,但它不工作。從Galley和照相機拍攝照片在Tamarin.forms

using System; 
using System.Threading.Tasks; 

using Xamarin.Forms; 

using XLabs.Platform.Device; 
using XLabs.Platform.Services.Media; 

namespace CalbrenEnterprises.Pages 
{ 
    public class TestPage : ContentPage 
    { 
     private ImageSource imageSource; 
     private IMediaPicker mediaPicker; 
     private Image img; 
     private string status; 

     public TestPage() 
     { 
      this.Title = "Camera Test"; 


      NavigationPage.SetHasNavigationBar(this, false); 


      img = new Image() { HeightRequest = 300, WidthRequest = 300, BackgroundColor = Color.FromHex("#D6D6D2"), Aspect = Aspect.AspectFit }; 


      var addPictureButton = new Button() 
      { 
       Text = "Select Picture", 
       Command = new Command(async() => { await SelectPicture(); }) 
      }; 


      StackLayout stack = new StackLayout(); 
      stack.VerticalOptions = LayoutOptions.FillAndExpand; 

      stack.Children.Add(new BoxView { Color = Color.Transparent, HeightRequest = 20 }); 
      stack.Children.Add(addPictureButton); 
      stack.Children.Add(img); 


      ScrollView scrollview = new ScrollView 
      { 
       Orientation = ScrollOrientation.Vertical, 
       VerticalOptions = LayoutOptions.FillAndExpand, 
       Content = stack 

      }; 

      this.Content = new StackLayout 
      { 
       Children = { scrollview } 
      }; 


     } 


     private async Task SelectPicture() 
     { 

      mediaPicker = DependencyService.Get<IMediaPicker>(); 

      imageSource = null; 

      try 
      { 
       var mediaFile = await mediaPicker.SelectPhotoAsync(new CameraMediaStorageOptions 
       { 
        DefaultCamera = CameraDevice.Front, 
        MaxPixelDimension = 400 
       }); 
       imageSource = ImageSource.FromStream(() => mediaFile.Source); 
       img.Source = imageSource; 
      } 
      catch (System.Exception ex) 
      { 
       this.status = ex.Message; 
      } 
     } 



    } 
} 

問:

我怎樣才能從圖庫中選擇照片和拍攝照片從相機PCL項目Xamarin.forms?

+0

也許,你應該張貼,究竟不起作用(並且如果存在錯誤後的消息),以便有人可以幫助你:-) – FredyWenger

回答

1
var device = Resolver.Resolve<IDevice>(); 
mediaPicker = DependencyService.Get<IMediaPicker>() ?? device.MediaPicker; 
if (mediaPicker == null) throw new NullReferenceException("MediaPicker DependencyService.Get error"); 

try 
{ 
    if (mediaPicker.IsCameraAvailable) 
    { 
     var options = new CameraMediaStorageOptions() { 
      DefaultCamera = CameraDevice.Front, 
      SaveMediaOnCapture = true, 
      Directory = "YourAppName", 
      Name = string.Format("YourAppName_{0}", DateTime.Now.ToString("yyMMddhhmmss")), 
      MaxPixelDimension = 1024, 
      PercentQuality = 85 
     }; 

     var mediaFile = await mediaPicker.TakePhotoAsync(options); 

     if (mediaFile != null && mediaFile.Source != null) 
     { 
      // do something with your photo 
     } 
    } 
    else 
    { 
     Logger.Info("Camera not available"); 
    } 
} 
catch (TaskCanceledException) 
{ 
    Logger.Info("TakePhoto cancelled"); 
} 
catch (Exception ex) 
{ 
    Logger.Error(ex); 
} 
+0

Hi..I想使用的SelectPhoto方法var selectedPhoto = await mediaPicker.SelectPhotoAsync(options);這不是什麼回味 – pritam001

相關問題