3

我正在编写需要获取设备位置的 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 的某些部分,我没有包含在代码段中,并在此页面的数据源初始化某处发现了原因。我很抱歉误报。

4

1 回答 1

1

您的代码对我来说很好,尝试经典的重启 VS 和项目!

该代码应该可以工作,并使用模拟器和设备(诺基亚 820)对其进行了测试。

祝你好运

于 2012-12-27T09:23:51.620 回答