1

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.

Community
  • 1
  • 1
user1667474
  • 819
  • 6
  • 24
  • 37

2 Answers2

1

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> (); 
Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
  • Looks like it could be a good solution. Although, the question is posed in the Android platform. Have any samples for that? – jensendp Sep 01 '14 at 04:35
  • Android would be just the same – Sten Petrov Sep 01 '14 at 04:43
  • Here's another answer with a specific example: http://stackoverflow.com/questions/25185903/xamarin-forms-labs-camera-permanently-saving-images-and-calling-them/25186674#25186674 – Sten Petrov Sep 01 '14 at 04:44
  • Hi, i had written my (obviously not very good because it doesn't work!) solution before I saw this. It is nearly working, I have posted another question here http://stackoverflow.com/questions/25601623/xamarin-forms-dependencyservice-difficulty-getting-location – user1667474 Sep 01 '14 at 08:23
  • I looked at your other question and you're still using Dependency service, which in my experience doesn't work very well. Try using Resolver as I spelled it out here and your MyLocation has to implement interface ICurrentLocation (from your other question) – Sten Petrov Sep 02 '14 at 00:30
0

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/

jensendp
  • 2,135
  • 15
  • 15
  • Thanks for your reply. i have tried implementing this, and a few others but I guess that I am not experienced enough in Xamarin (I started using it a week ago) to be able to convert this to getting the location. – user1667474 Aug 31 '14 at 23:25
  • I would be happy to help you out a little bit more. I will try to put together some sample code and include it in my answer. By the way, are you using a Xamarin.Forms "PCL" project template or a "Shared Library" project template? – jensendp Sep 01 '14 at 03:31
  • You can also use the Xamarin.Forms.Maps package from nuget as suggested in the Xamarin forums here: http://forums.xamarin.com/discussion/17302/xamarin-forms-maps – jensendp Sep 01 '14 at 04:34
  • HI (and thankyou), I have actually had a little bit of success. I am able to run it using but it isn't returning the object (getting a SystemNullReference Exception: Object Not set to an instance of an object. I will do a new post, hopefully you can easily see the error(s) that I have made _ I will post the link to it here – user1667474 Sep 01 '14 at 06:15
  • Now i get the object back but too early - new Q posted here - it would be great if you could have a look and tell me what you reckon http://stackoverflow.com/questions/25601623/xamarin-forms-dependencyservice-difficulty-getting-location cheers – user1667474 Sep 01 '14 at 08:31