user1667474
user1667474

Reputation: 819

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

I have tried, without luck, to get the current location from within the Xamarin.Forms code. I tried the Mobile Services for Xamarin.Forms from their sample code (found here http://github.com/aritchie/acr-xamarin-forms) but it is very complex and there are no comments, so it is not easy to follow. I can get it in the device specific code (in this instance Android), but only in an Activity, and I have no idea how to get it back to the Forms. As Xamarin.Forms are so new there is not much documentation, only references to what is possible.

I have looked at this: Write device platform specific code in Xamarin.Forms but I don't think it is what I am after as I need to be able to call it, not get it on start-up (I may be completely wrong here though, I have only been using Xamarin for a little over a week, and Xamarin.Forms for 2 days)

I am tearing my hair out, so any suggestions will be greatly appreciated.

Upvotes: 1

Views: 941

Answers (2)

Sten Petrov
Sten Petrov

Reputation: 11040

Take a look at Xamarin.Forms.Labs.Resolver class, add the Labs package from NuGet.

To use it make an interface in your Forms project IMyService, in the iOS/Android project make 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 call Resolver.Resolve:

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

Upvotes: 1

jensendp
jensendp

Reputation: 2135

Typically when you are using Xamarin.Forms and you want to access platform specific functionality, you will want to use a Dependency Service that will allow you to write code in the platform specific project to use special platform features or APIs. You can then reference this service from the Xamarin.Forms project and allows you to jump back and forth. There is some very good documentation on the Xamarin site for this.

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

Upvotes: 0

Related Questions