我需要使用Xamarin Forms PCL访问相机。 我无法找到任何可行的样本。
我尝试安装XLab并使用其示例代码,但它有例外。例如,使用XLabs示例我使用以下内容:
/// <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;
}
我在Resolver.Resolve<IDevice>():
IResolver has not been set. Please set it by calling Resolver.SetResolver(resolver)
我如何使用这个或其他替代方法用Xamarin.Forms拍照?感谢提前提示!
答案 0 :(得分:5)
在您的Droid项目中添加MainActivity类代码
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
....
}
在IOS类AppDelegate中这一个:
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
....
}
答案 1 :(得分:2)
以下是xLabs Camera的示例代码:
https://www.dropbox.com/s/9paojg1q43ahnqk/XPA_PickMedia_XLabs_XFP.zip?dl=0
在论坛上找到它:https://forums.xamarin.com/discussion/35636/a-sample-project-using-camera-control-mediapicker-of-xlabs
答案 2 :(得分:1)
您无法直接访问Xamarin.Forms中的本机功能。您必须使用依赖服务来访问本机功能,例如访问Camrera,音频等。
试试这个:http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/
答案 3 :(得分:0)
您必须配置IoC才能使其正常工作,并使用Resolver.SetResolver方法将IoC提供程序设置为Resolver。在此处阅读更多内容,https://github.com/XLabs/Xamarin-Forms-Labs/wiki/IOC