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

后端 未结 2 716
北恋
北恋 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条回答
  •  -上瘾入骨i
    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(t=>new MyServiceImplementorClass()); 
      Resolver.SetResolver (resolverContainer.GetResolver ());
    }
    

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

    IMyService fileAccess = Resolver.Resolve (); 
    

提交回复
热议问题