简体   繁体   中英

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!

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.

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