Reputation: 1627
I need to access the camera in with Xamarin Forms PCL. I couldn't find any sample that works.
I tried by installing XLab and using its sample code but it has exceptions. For instance, with the XLabs sample I am using the following:
/// <summary>
/// Takes the picture.
/// </summary>
/// <returns>Take Picture Task.</returns>
internal async Task<MediaFile> TakePicture()
{
Setup();
ImageSource = null;
return await _mediaPicker.TakePhotoAsync(new CameraMediaStorageOptions { DefaultCamera = CameraDevice.Front, MaxPixelDimension = 400 }).ContinueWith(t =>
{
if (t.IsFaulted)
{
Status = t.Exception.InnerException.ToString();
}
else if (t.IsCanceled)
{
Status = "Canceled";
}
else
{
var mediaFile = t.Result;
ImageSource = ImageSource.FromStream(() => mediaFile.Source);
return mediaFile;
}
return null;
}, _scheduler);
}
/// <summary>
/// Setups this instance.
/// </summary>
private void Setup()
{
if (_mediaPicker != null)
{
return;
}
var device = Resolver.Resolve<IDevice>();
////RM: hack for working on windows phone?
_mediaPicker = DependencyService.Get<IMediaPicker>() ?? device.MediaPicker;
}
I get the following exception on Resolver.Resolve<IDevice>():
IResolver has not been set. Please set it by calling Resolver.SetResolver(resolver)
How can I use this or other alternative to take picture with Xamarin.Forms? thanks for any tip in advance!
Upvotes: 2
Views: 5127
Reputation: 3846
Add in your Droid Project, class MainActivity this code
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
#region Resolver Init
SimpleContainer container = new SimpleContainer();
container.Register<IDevice>(t => AndroidDevice.CurrentDevice);
container.Register<IDisplay>(t => t.Resolve<IDevice>().Display);
container.Register<INetwork>(t => t.Resolve<IDevice>().Network);
Resolver.SetResolver(container.GetResolver());
#endregion
....
}
and in IOS class AppDelegate this one:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
#region Resolver Init
SimpleContainer container = new SimpleContainer();
container.Register<IDevice>(t => AppleDevice.CurrentDevice);
container.Register<IDisplay>(t => t.Resolve<IDevice>().Display);
container.Register<INetwork>(t => t.Resolve<IDevice>().Network);
Resolver.SetResolver(container.GetResolver());
#endregion
....
}
Upvotes: 5
Reputation: 1261
Here is a sample code for xLabs Camera:
https://www.dropbox.com/s/9paojg1q43ahnqk/XPA_PickMedia_XLabs_XFP.zip?dl=0
Found it from forum over here: https://forums.xamarin.com/discussion/35636/a-sample-project-using-camera-control-mediapicker-of-xlabs
Upvotes: 2
Reputation: 2639
You can not directly access native functionality in Xamarin.Forms. You have to user Dependency Service to access native functionality like accessing Camrera, audio etc.
Try this: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/
Upvotes: 1
Reputation: 1545
You have to configure IoC to get it working, and set the IoC provider to the Resolver with the Resolver.SetResolver method. Read more here, https://github.com/XLabs/Xamarin-Forms-Labs/wiki/IOC
Upvotes: 0