Xamarin.Forms get data from device specific code back to forms

I have tried, with no luck, getting the current location from the Xamarin.Forms code. I tried Mobile Services for Xamarin.Forms from my example code (here http://github.com/aritchie/acr-xamarin-forms ) but it is very difficult and no comments so not easy to follow. I can get it in device code (in this case Android), but only in Activity, and I don't know how to return it to forms. Since Xamarin.Forms is so new that there is not enough documentation, only links to what is possible.

I looked at this: Write device platform code in Xamarin.Forms , but I don't think this is what I get as I need to be able to call it and not get it on startup (I might be completely wrong here, although, I only used Xamarin for a little over a week and Xamarin.Forms for 2 days)

I will rip my hair out so any suggestions would be greatly appreciated.

+1


source to share


2 answers


Take a look at the class Xamarin.Forms.Labs.Resolver

, add Labs package from NuGet.

To use it, create an interface in your Forms project IMyService, in your iOS / Android project create a class that implements IMyService.

In your AppDelegate register the service with Resolver



public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
  var resolverContainer = new SimpleContainer ();
  resolverContainer.Register<IMyService>(t=>new MyServiceImplementorClass()); 
  Resolver.SetResolver (resolverContainer.GetResolver ());
}

      

Then wherever you need to use your service in Forms mode, call Resolver.Resolve:

IMyService fileAccess = Resolver.Resolve<IMyService> (); 

      

+1


source


Typically when you are using Xamarin.Forms and you want to access platform functionality, you will want to use a dependency service, which will allow you to write code in a specific platform project to use platform specific functions or APIs. Then you can reference this service from the Xamarin.Forms project and lets you hop back and forth. There is very good documentation for this on the Xamarin.



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

0


source







All Articles