Ios 启动StartUpdatingLocation()后,每次更新位置时都运行另一个方法

Ios 启动StartUpdatingLocation()后,每次更新位置时都运行另一个方法,ios,xamarin,xamarin.forms,Ios,Xamarin,Xamarin.forms,启动StartUpdatingLocation()后,每次更新位置时都运行另一个方法 我试图在public LocationManager() 但是数据只发送到服务器一次 public LocationManager() { this.locMgr = new CLLocationManager { PausesLocationUpdatesAutomatically = false

启动StartUpdatingLocation()后,每次更新位置时都运行另一个方法

我试图在
public LocationManager()

但是数据只发送到服务器一次

public LocationManager()
        {
           this.locMgr = new CLLocationManager
           {
               PausesLocationUpdatesAutomatically = false
           };
           locMgr.AllowsBackgroundLocationUpdates = true;
           locMgr.DesiredAccuracy = 1;
           locMgr.DistanceFilter = 1;
           locMgr.StartUpdatingLocation();
}

async public void MethodToRun(){ 
// here I send the data to the server
// code ...
}

我需要在每次更新位置数据时发送数据。

您需要实现一个
ICLLocationManagerDelegate
实例,并将其分配给您创建的
CLLocationManager
实例

ICLLocationManagerDelegate
中,是来自名为
UpdatedLocation
的位置管理器的回调(ObjC:
locationManager:didUpdateToLocation:fromLocation:

因此,将
ICLLocationManagerDelegate
添加到现有类中,或者创建一个新类,并实现
UpdatedLocation

public class ALocationDelegate : NSObject, ICLLocationManagerDelegate
{
    [Export("locationManager:didUpdateToLocation:fromLocation:")]
    public void UpdatedLocation(CLLocationManager manager, CLLocation newLocation, CLLocation oldLocation)
    {
        // do something with the updated location (newLocation)
    }
}
现在,在您的CLLocationManager实例上,为该委托分配一个现有实例(或创建一个新实例):

locMgr.Delegate = new ALocationDelegate();
关于:

locMgr.Delegate = new ALocationDelegate();