简体   繁体   中英

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.

I have this setup the same, but when I run my app the iOS app that calls my PCL is giving the error:

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. How can I get this camera available to my PCL when I'm not using MVC here.

If you wish to use the ViewModel then modify the constructor to take in IMediaPicker:

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

How you get the IMediaPicker implementation to PCL is up to you. IoC is the preferred (and easy) way but you can use other patterns as well. One would be to define a static Func<IMediaPicker> property on the ViewModel and set it in the platform specific projects.

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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