2

我需要使用 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 拍照?提前感谢您的任何提示!

4

4 回答 4

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
    ....
}
于 2016-06-25T09:28:24.513 回答
2

这是 xLabs 相机的示例代码:
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

于 2015-07-23T15:47:21.827 回答
1

您不能直接访问 Xamarin.Forms 中的本机功能。您必须使用Dependency Service才能访问本机功能,例如访问 Camrera、音频等。

试试这个:http: //developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/

于 2015-07-12T16:09:47.620 回答
0

您必须配置 IoC 以使其工作,并使用 Resolver.SetResolver 方法将 IoC 提供程序设置为 Resolver。在此处阅读更多信息,https://github.com/XLabs/Xamarin-Forms-Labs/wiki/IOC

于 2015-07-11T12:19:04.057 回答