Search code examples
androidwindows-phone-8cameraxamarin

Xamarin.Forms IMediaPicker WinPhone


I working on an app that will run on Android and Windows Phone 8.0. This app must to be able to take photos. I found an easy way to do that by using IMediaPicker. However, it only works on Android. On WinPhone the code below returns null.

mediaPicker = DependencyService.Get<IMediaPicker>();

How can I get IMediaPicker on WinPhone? Any example?

Thanks in advance!


Solution

  • I found the problem. On WinPhone mediaPicker will be null. Then, it's necessary to get it from Resolver.

    if (mediaPicker == null)
    {
        mediaPicker = Resolver.Resolve<IDevice>().MediaPicker;
    }
    

    But, for this to work, it's necessary to initiate the Resolver on WinPhone app constructor, like this:

    var resolverContainer = new SimpleContainer();
    resolverContainer.Register<IDevice>(t => WindowsPhoneDevice.CurrentDevice).Register<IDisplay>(t => t.Resolve<IDevice>().Display);
    
    Resolver.SetResolver(resolverContainer.GetResolver());
    

    Hope it helps someone else.