简体   繁体   English

Xamarin XLabs IOC和相机

[英]Xamarin XLabs IOC and camera

I'm looking at https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs to try and get the camera available to my app. 我正在查看https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs尝试将相机提供给我的应用程序。

I have this setup the same, but when I run my app the iOS app that calls my PCL is giving the error: 我的设置与此相同,但是当我运行我的应用时,调用我的PCL的iOS应用给出了错误消息:

IResolver has not been set. Please set it by calling Resolver.SetResolver(resolver) method.

I don't know what this means or what exact code I need. 我不知道这意味着什么或我需要什么确切的代码。 I don't use IOC at all and I don't much care about it but I just want this camera to be available to my PCL. 我根本不使用IOC,也不太在意它,但我只是希望我的PCL可以使用此相机。 How can I get this camera available to my PCL when I'm not using MVC here. 当我在这里不使用MVC时,如何使此相机可供PCL使用。

If you wish to use the ViewModel then modify the constructor to take in IMediaPicker: 如果希望使用ViewModel,则修改构造函数以使用IMediaPicker:

https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs#L62 https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs#L62

    public CameraViewModel(IMediaPicker mediaPicker)
    {
        this._mediaPicker = mediaPicker;
    }

Remove the Setup function: 删除设置功能:

https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs#L156-L167 https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs#L156-L167

How you get the IMediaPicker implementation to PCL is up to you. 如何将IMediaPicker实现实现到PCL由您决定。 IoC is the preferred (and easy) way but you can use other patterns as well. IoC是首选(且简便)的方式,但您也可以使用其他模式。 One would be to define a static Func<IMediaPicker> property on the ViewModel and set it in the platform specific projects. 一种方法是在ViewModel上定义一个静态Func<IMediaPicker>属性,并将其设置在特定于平台的项目中。

IoC container is the easiest and quickest way to get it done and it would as simple as adding these lines at the application startup (eg AppDelegate's FinishedLaunching method): IoC容器是完成它的最简单,最快的方法,它就像在应用程序启动时添加这些行一样简单(例如AppDelegate的FinishedLaunching方法):

var container = new SimpleContainer();
container.Register<IMediaPicker, MediaPicker>();
Resolver.SetResolver(container.GetResolver());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM