如何使用Xamarin在Android中同步获取GPS位置更新?

如何使用Xamarin在Android中同步获取GPS位置更新?,第1张

概述具体来说,我正在使用Xamarin.Forms进行C#开发,但是在本机Android方面工作,编写了GPS包装类,该类可以通过依赖注入在Xamarin.Forms方面使用.在大多数情况下,关于Android,C#和Java之间的调用应该相同.本质上,我在Android端的Geolocator对象(实现ILocationListener)中具有此方法:pub

具体来说,我正在使用Xamarin.Forms进行C#开发,但是在本机Android方面工作,编写了GPS包装类,该类可以通过依赖注入在Xamarin.Forms方面使用.在大多数情况下,关于AndroID,C#和Java之间的调用应该相同.

本质上,我在AndroID端的Geolocator对象(实现ILocationListener)中具有此方法:

public async Task<Tuple<bool, string, GPSData>> GetGPSData() {        gpsData = null;        var success = false;        var error = string.Empty;        if (!manager.IsProvIDerEnabled(LocationManager.GpsProvIDer)) {            //request permission or location services enabling            //set error        } else {            manager.RequestSingleUpdate(LocationManager.GpsProvIDer, this, null);            success = true;        }        return new Tuple<bool, string, GPSData>(success, error, gpsData); }

 public voID OnLocationChanged(Location location) {        gpsData = new GPSData(location.Latitude, location.Longitude);    }

我希望能够调用GetGPSData并让它返回元组,目前关于元组的唯一重要的事情是gpsData已被填充.我知道找到修复方法可能需要几秒钟,因此我希望此方法是异步的一旦我真正需要该值,就可以在Xamarin.Forms端等待.

我的问题是我想不出一种方法来让manager.RequestSingleUpdate同步工作或进行其他工作.您调用该方法,然后最终触发OnLocationChanged.我试图投掷令人作呕的野蛮人

 while (gpsData == null);

在强制它在OnLocationChanged被触发之前不要继续进行的调用之后,但是当我将该行放入时,永远不会调用OnLocationChanged.我假设这是因为OnLocationChanged是在同一线程而不是后台线程上调用的.

我有什么办法可以采取这种情况,并在OnLocationChanged触发之前不返回GetGPSData?

谢谢

编辑:要添加,此方法将不会定期调用.它是自发的,很少见,所以我不想使用RequestLocationUpdates,获取常规更新并返回最新的更新,因为这将需要始终打开GPS,而不必要地给电池下雨.

解决方法:

您可以使用taskcompletionsource执行所需的 *** 作.我遇到了同样的问题,这就是我解决的方法:

taskcompletionsource<Tuple<bool, string, GPSData> tcs;// No need for the method to be async, as nothing is await-ed insIDe it.public Task<Tuple<bool, string, GPSData>> GetGPSData() {    tcs = new taskcompletionsource<Tuple<bool, string, GPSData>>();    gpsData = null;    var success = false;    var error = string.Empty;    if (!manager.IsProvIDerEnabled(LocationManager.GpsProvIDer)) {        //request permission or location services enabling        //set error        tcs.TrySetException(new Exception("some error")); // This will throw on the await-ing caller of this method.    } else {        manager.RequestSingleUpdate(LocationManager.GpsProvIDer, this, null);        success = true;    }    //return new Tuple<bool, string, GPSData>(success, error, gpsData); <-- change this to:    return this.tcs.Task;}

和:

public voID OnLocationChanged(Location location) {        gpsData = new GPSData(location.Latitude, location.Longitude);        // Here you set the result of taskcompletionsource. Your other method completes the task and returns the result to its caller.        tcs.TrySetResult(new Tuple<bool, string, GPSData>(false, "someString", gpsData));    }
总结

以上是内存溢出为你收集整理的如何使用Xamarin在Android中同步获取GPS位置更新?全部内容,希望文章能够帮你解决如何使用Xamarin在Android中同步获取GPS位置更新?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1120767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存