【发布时间】:2016-12-31 15:48:11
【问题描述】:
我正在使用 Xamarin 构建一个应该使用位置服务的 Android 应用程序。
在 OnCreate 中,我构建了 GoogleApiClient:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (new KMN.App ());
apiClient = new GoogleApiClient.Builder(this, this, this).AddApi(LocationServices.API).Build();
apiClient.Connect();
}
apiClient 之后是 != null 。比我进入:
public void OnConnected(Bundle connectionHint)
{
LocationRequest locRequest = new LocationRequest();
locRequest.SetPriority(LocationRequest.PriorityBalancedPowerAccuracy);
locRequest.SetFastestInterval(500);
locRequest.SetInterval(1000);
LocationServices.FusedLocationApi.RequestLocationUpdates(apiClient, locRequest, this);
}
这里 apiClient 仍然是 != null。
这个方法永远不会被调用:
public void OnLocationChanged(Android.Locations.Location location)
{
LastLocation = location;
}
当我从 UI 调用此方法时,apiClient 为空:
public Adresse getAdresse()
{
if (LastLocation!= null)
{
return new Adresse()
{
Latitude = LastLocation.Latitude,
Longitude = LastLocation.Longitude
};
}
else
{
return new Adresse()
{
Latitude = 0,
Longitude = 0
};
}
}
【问题讨论】:
-
我假设您通过表单的依赖服务调用
getAdresse?你在哪里定义apiClient?如果它在您的MainActivity类中,并且您也在为getAdresse实现 DS 接口,您需要重新考虑您的方法,因为您正在“重新创建”您的MainActivity类 -
你是对的。我通过 DependencyService 调用 getAddresse,并且在我的 MainActivity 中定义了 apiClient。正确的方法是什么?
标签: android xamarin xamarin.android google-play-services