如何正确实现对xamarin表单的位置更改?
我有一个监听器,每隔10秒或100米左右就能捕捉到这个位置。我正在使用 xam.plugin.geologitor 实现侦听器。我的问题是,当我的应用程序最小化或应用程序打开但手机锁定时,位置侦听器不工作(意味着位置更改未捕获或保存在位置缓存中) 这是我的密码:如何正确实现对xamarin表单的位置更改?,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,我有一个监听器,每隔10秒或100米左右就能捕捉到这个位置。我正在使用 xam.plugin.geologitor 实现侦听器。我的问题是,当我的应用程序最小化或应用程序打开但手机锁定时,位置侦听器不工作(意味着位置更改未捕获或保存在位置缓存中) 这是我的密码: async Task StartListening() { if (!CrossGeolocator.Current.IsListening) { var defaultg
async Task StartListening()
{
if (!CrossGeolocator.Current.IsListening)
{
var defaultgpsaccuracy = Convert.ToDouble(Preferences.Get("gpsaccuracy", String.Empty, "private_prefs"));
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(10), defaultgpsaccuracy, false, new Plugin.Geolocator.Abstractions.ListenerSettings
{
ActivityType = Plugin.Geolocator.Abstractions.ActivityType.Other,
AllowBackgroundUpdates = true,
DeferLocationUpdates = true,
DeferralDistanceMeters = 1,
DeferralTime = TimeSpan.FromSeconds(1),
ListenForSignificantChanges = true,
PauseLocationUpdatesAutomatically = false
});
}
}
我将此代码放在我的login.xaml.cs
以下是我的问题:
首先需要初始化StartListening,然后为位置更改和错误处理创建事件处理程序
public Position CurrentPosition{get;set;}
公共事件处理程序已更改;
不要忘记在构造函数中初始化它:
CurrentPosition=新位置()代码>
等待CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(20),10,true);
CrossGeolocator.Current.PositionChanged+=位置更改;
CrossGeolocator.Current.PositionError+=位置错误代码>
职能:
`private void PositionChanging(object sender, PositionEventArgs e)
{
CurrentPosition = e.Position;
if (PositionChanged != null)
{
PositionChanged(this, null);
}
}
private void PositionError(object sender, PositionErrorEventArgs e)
{
Debug.WriteLine(e.Error);
}`
您现在可以在需要最新职位时调用PositionChanged
别忘了停止倾听:
`public async Task StopListeningAsync()
{
if (!CrossGeolocator.Current.IsListening)
return;
await CrossGeolocator.Current.StopListeningAsync();
CrossGeolocator.Current.PositionChanged -= PositionChanging;
CrossGeolocator.Current.PositionError -= PositionError;
}`
首先需要初始化StartListening,然后为位置更改和错误处理创建事件处理程序
public Position CurrentPosition{get;set;}
公共事件处理程序已更改;
不要忘记在构造函数中初始化它:
CurrentPosition=新位置()代码>
等待CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(20),10,true);
CrossGeolocator.Current.PositionChanged+=位置更改;
CrossGeolocator.Current.PositionError+=位置错误代码>
职能:
`private void PositionChanging(object sender, PositionEventArgs e)
{
CurrentPosition = e.Position;
if (PositionChanged != null)
{
PositionChanged(this, null);
}
}
private void PositionError(object sender, PositionErrorEventArgs e)
{
Debug.WriteLine(e.Error);
}`
您现在可以在需要最新职位时调用PositionChanged
别忘了停止倾听:
`public async Task StopListeningAsync()
{
if (!CrossGeolocator.Current.IsListening)
return;
await CrossGeolocator.Current.StopListeningAsync();
CrossGeolocator.Current.PositionChanged -= PositionChanging;
CrossGeolocator.Current.PositionError -= PositionError;
}`
您需要在原生Xamarin.Android代码中实现一个foregunded服务,该代码包括您的位置代码(但没有Xamarin.Forms引用,因为该代码不能依赖于初始化的表单应用程序)@SushiHangover你能给我举个例子吗?我评论中的链接是指向Xamarin docs关于在Androdi上启动前置服务器的链接。你需要在原生Xamarin.Android代码中实现前置服务,其中包括你的位置代码(但是没有Xamarin.Forms引用,因为代码不能依赖于初始化的Forms应用程序)@SushiHangover你能给我举个例子说明如何做吗?我的评论中的链接是Xamarin docs关于在Android上启动前置服务器的文章如果我把这个函数放在我的App.xaml.cs的OnSleep()下怎么办应用程序还会侦听吗?如果我将App.xaml.cs中的函数放在OnSleep()下,应用程序还会侦听吗?