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

后端 未结 2 715
北恋
北恋 2021-01-25 12:05

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

相关标签:
2条回答
  • 2021-01-25 12:40

    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/

    0 讨论(0)
  • 2021-01-25 12:46

    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> (); 
    
    0 讨论(0)
提交回复
热议问题