异步和位置侦听器有问题
Having trouble with Async and Location Listener
我查看了 MSDN 和此处,并尝试了多种不同的方法来完成这项工作 none。我正在尝试启动 James Montemagno 的 Xamarin 地理定位器插件的侦听器。即使使用他的确切代码,我也完全被卡住了。
我想一定有一些关于 await.async 我完全遗漏的东西,也许只是一般的 C# 中的 OO 编程。我是 Xamarin 和 C# 的新手,所以我有点挣扎。我有一个名为 LocationControls.cs 的 class,看起来像这样
using EIOBoardMobile.Model;
namespace EIOBoardMobile.CustomControls
{
public static class LocationControls
{
public static async Task StartListening()
{
if (CrossGeolocator.Current.IsListening)
return;
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromMinutes(5), 10, true);
CrossGeolocator.Current.PositionChanged += PositionChanged;
CrossGeolocator.Current.PositionError += PositionError;
}
private static void PositionChanged(object sender, PositionEventArgs e)
{
GPSLocationCurrent.SetLatLong();
}
private static void PositionError(object sender, PositionErrorEventArgs e)
{
Debug.WriteLine(e.Error);
//Handle event here for errors
}
private static async Task StopListening()
{
if (!CrossGeolocator.Current.IsListening)
return;
await CrossGeolocator.Current.StopListeningAsync();
CrossGeolocator.Current.PositionChanged -= PositionChanged;
CrossGeolocator.Current.PositionError -= PositionError;
}
}
}
我想在应用程序打开时立即启动侦听器。所以我的 App.xaml.cs 看起来像这样。
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
if (Settings.LocationTracking == true)
{
await LocationControls.StartListening();
//this is where I'm getting the error
}
}
错误说我只能将 await 与异步方法一起使用。但是如果我尝试不使用 await 它说我不能使用没有 await 关键字的方法。
我也尝试过不使用 static 关键字,但它说它需要一个对象引用。但是当我尝试创建一个对象引用时,它说它没有 return 任何东西。我尝试的每一种不同方式都会出错。我只想能够立即启动监听器。我意识到 StartListening() 中 if 之后的 return 语句阻止我为 StartListening() 方法提供 return 类型,但我不想要 return 类型。如果它已经在监听,我需要一种不 运行 StartListeningAsync 的方法,所以我需要 return.
我对异步方法很迷茫。也许如果我只是了解如何为非静态方法做一个对象引用会有所帮助。但我试过使用变量,但没有用。
构造函数不能是异步的,因此在其中调用异步方法可能会出现问题。最简单的解决方法是从 App 的 OnStart() 调用它,这可以通过 async
protected async override void OnStart()
{
if (Settings.LocationTracking == true)
{
await LocationControls.StartListening();
}
}
我查看了 MSDN 和此处,并尝试了多种不同的方法来完成这项工作 none。我正在尝试启动 James Montemagno 的 Xamarin 地理定位器插件的侦听器。即使使用他的确切代码,我也完全被卡住了。
我想一定有一些关于 await.async 我完全遗漏的东西,也许只是一般的 C# 中的 OO 编程。我是 Xamarin 和 C# 的新手,所以我有点挣扎。我有一个名为 LocationControls.cs 的 class,看起来像这样
using EIOBoardMobile.Model;
namespace EIOBoardMobile.CustomControls
{
public static class LocationControls
{
public static async Task StartListening()
{
if (CrossGeolocator.Current.IsListening)
return;
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromMinutes(5), 10, true);
CrossGeolocator.Current.PositionChanged += PositionChanged;
CrossGeolocator.Current.PositionError += PositionError;
}
private static void PositionChanged(object sender, PositionEventArgs e)
{
GPSLocationCurrent.SetLatLong();
}
private static void PositionError(object sender, PositionErrorEventArgs e)
{
Debug.WriteLine(e.Error);
//Handle event here for errors
}
private static async Task StopListening()
{
if (!CrossGeolocator.Current.IsListening)
return;
await CrossGeolocator.Current.StopListeningAsync();
CrossGeolocator.Current.PositionChanged -= PositionChanged;
CrossGeolocator.Current.PositionError -= PositionError;
}
}
}
我想在应用程序打开时立即启动侦听器。所以我的 App.xaml.cs 看起来像这样。
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
if (Settings.LocationTracking == true)
{
await LocationControls.StartListening();
//this is where I'm getting the error
}
}
错误说我只能将 await 与异步方法一起使用。但是如果我尝试不使用 await 它说我不能使用没有 await 关键字的方法。
我也尝试过不使用 static 关键字,但它说它需要一个对象引用。但是当我尝试创建一个对象引用时,它说它没有 return 任何东西。我尝试的每一种不同方式都会出错。我只想能够立即启动监听器。我意识到 StartListening() 中 if 之后的 return 语句阻止我为 StartListening() 方法提供 return 类型,但我不想要 return 类型。如果它已经在监听,我需要一种不 运行 StartListeningAsync 的方法,所以我需要 return.
我对异步方法很迷茫。也许如果我只是了解如何为非静态方法做一个对象引用会有所帮助。但我试过使用变量,但没有用。
构造函数不能是异步的,因此在其中调用异步方法可能会出现问题。最简单的解决方法是从 App 的 OnStart() 调用它,这可以通过 async
protected async override void OnStart()
{
if (Settings.LocationTracking == true)
{
await LocationControls.StartListening();
}
}