geolocation - 首次启动后 Geolocator.GetGeopositionAsync() 的奇怪行为

标签 geolocation windows-phone-8

我正在编写需要获取设备位置的 Windows Phone 8 应用(不跟踪更改,只获取位置)。我在起始页的 OnNavigatedTo() 方法中添加了下一个代码,但在启动应用程序后,进度指示器即使在 10 秒超时后也不会隐藏。但是,如果我导航到另一个页面然后返回,一切正常。这发生在模拟器上,我没有真实的设备。我做错了什么?

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    if(_geoPosition == null)
    {
        try
        {
            var geolocator = new Geolocator();
            geolocator.DesiredAccuracyInMeters = 50;

            _progressIndicator = new ProgressIndicator
            {
                IsIndeterminate = true,
                Text = "Getting current location, please wait...",
                IsVisible = true
            };
            SystemTray.SetIsVisible(this, true);
            SystemTray.SetProgressIndicator(this, _progressIndicator);

            _geoPosition = await geolocator.GetGeopositionAsync(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10));

            _progressIndicator.IsVisible = false;
            SystemTray.SetIsVisible(this, false);
        }
        catch (UnauthorizedAccessException)
        {
            MessageBox.Show("Location is disabled in phone settings");
        }
    }
}

谢谢!

UPD:刚刚尝试将这段代码添加到空项目中,它运行良好。试图注释掉 OnNavigatedTo 的一些我没有包含在代码片段中的部分,并发现原因在于此页面的数据源初始化中的某处。对不起,误报了。

最佳答案

你的代码对我来说工作正常,尝试经典重启 VS 和项目!代码应该工作,用模拟器和设备(诺基亚 820)测试它。

祝你好运

关于geolocation - 首次启动后 Geolocator.GetGeopositionAsync() 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14051681/

相关文章:

ios - 未调用 didDetermineState、didEnterRegion、didExitRegion 事件

c# - VS 2015-WP8- "A specified communication resource (port) is already in use by another application."

windows - 如何使用 Phonegap/Cordova/WinJS/HTML/CSS/JS 在 Windows Phone 8.1 中扫描二维码?

c# - 如何向编码的 UI 测试 (Windows Phone 8.1) 项目添加功能(在 C# 中)

java - 使用 GPS(位置 API)时出现位置异常

database - 根据距离和受欢迎程度对地点进行排名

xaml - Windows Phone工具包ListPicker只允许5个元素

visual-studio - 没有可启动的项目可用于分析

c# - 如何解决 Windows Phone 8 应用程序中的以下错误?

javascript - 地理位置始终显示莫斯科 Javascript/JQuery/JSON(开放天气 API)