我正在尝试在GPS不可用时提醒用户(基本上当他们关闭地理位置服务时)
以下是我的查询案例陈述,我注意到在模拟器中我点击了“”状态,因此我无法在此处返回错误,因为它仍然按预期工作
void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
client.ClientCallBackWithError("fail");
break;
case GeoPositionStatus.Initializing:
var y = "initializing location service,";
break;
case GeoPositionStatus.NoData:
// The location service is working, but it cannot get location data
// Alert the user and enable the Stop Location button
var z = "data unavailable,";
break;
case GeoPositionStatus.Ready:
var zzz = "receiving data, ";
break;
}
}
所以在我的调用页面/视图中,我决定也许我可以等待10秒钟,看看是否曾经被击中..如果不是,我可能会抛出错误/等等
警告巨大的黑客,因为我失去了希望
private void FindByGps_Click(object sender, RoutedEventArgs e)
{
gpsStarted = false;
gpsEnded = false;
progressHelper.StartProgressStuff(this.progress);
gpsStarted = true;
gpsLocationLookupService.StartLocationService();
this.Dispatcher.BeginInvoke(() => ListenForCallbackDuringGpsLookup(0));
}
private object ListenForCallbackDuringGpsLookup(int counter)
{
if (gpsStarted && !gpsEnded && counter < 12)
{
//keep looking until the timer runs out ...
counter = counter + 1;
this.Dispatcher.BeginInvoke(() => SleepForASec());
ListenForCallbackDuringGpsLookup(counter);
}
else if (gpsStarted && gpsEnded)
{
gpsStarted = false;
gpsEnded = false;
}else{
this.Dispatcher.BeginInvoke(() => SetCurrentLocationAndLaunchFindKiosks(null, "Failed to locate you by GPS"));
}
return null;
}
private object SleepForASec()
{
Thread.Sleep(1000);
return null;
}
但第二个我启动了一个线程似乎锁定应用程序,直到线程化的东西完成。
所以我的问题 - 我应该如何捕获这种类型的gps错误来提供正确的反馈?
答案 0 :(得分:1)
你看过这个API吗?
根据此处的说法,您应该能够在以下框架内执行某些操作:
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher;
// Constructor
public MainPage()
{
InitializeComponent();
Loaded += (source, args) =>
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
watcher.PositionChanged += (sender, e) =>
{
// update the user
};
watcher.StatusChanged += (sender, e) =>
{
// update the user
};
if (!watcher.TryStart(false, TimeSpan.FromSeconds(5)))
{
// show the error somewhere
}
};
}
}
然后,在应用程序执行期间,如果GPS出现问题(丢失信号等),那么您可以在StatusChanged事件处理程序内做出适当的响应。
如果我在这里不合时宜,请告诉我,我会继续考虑更好的解决方案......