当位置服务关闭时,Windows 8手机应用程序在打开时崩溃

时间:2014-07-09 08:43:06

标签: c# api xaml windows-phone-8 geolocation

我有一个天气应用程序,它以用户位置的纬度和经度,然后使用api来回馈结果。关闭位置服务后,应用程序在打开时崩溃,并且没有错误帮助以查看错误的位置。有没有办法写一个if语句来查看位置服务是否是一个?我该怎么做才能防止这个问题?

以下是代码:

 async private void GetLocation()
        {
            var geolocator = new Geolocator();
            if (geolocator.LocationStatus == PositionStatus.Disabled)
            {
                //MessageBox.Show("We need your current location for the app to function properly, please set location services on in settings");
                MessageBoxResult mRes = MessageBox.Show("We need your current location for the app to function properly, please set location services on in settings", "I understand", MessageBoxButton.OKCancel);
                if (mRes == MessageBoxResult.OK)
                {
                    Application.Current.Terminate();
                }
                if (mRes == MessageBoxResult.Cancel)
                {
                    Application.Current.Terminate();
                }
            }

            Geoposition position = await geolocator.GetGeopositionAsync();
            Geocoordinate coordinate = position.Coordinate;
            latitude = Convert.ToString(Math.Round(coordinate.Latitude, 2));
            longitude = Convert.ToString(Math.Round(coordinate.Longitude, 2));

            URL = "http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&units=metric";

            Client(URL);
        }

public void Client(string uri)
        {
            var clientToken = new WebClient();
            clientToken.OpenReadCompleted += clientToken_OpenReadCompleted;
            clientToken.OpenReadAsync(new Uri(uri));
        }

感谢您的帮助:)

2 个答案:

答案 0 :(得分:0)

要确定错误发生的位置,请检查Microsoft从Windows开发人员中心记录的错误堆栈跟踪 - https://dev.windowsphone.com/en-US/CrashReport("导出最近30天内的顶部堆栈跟踪"链接上图)

堆栈跟踪可能会出现延迟。 BugSense是一个非常有用的工具,可以为您提供出色的错误报告,以帮助您进行调试。只需要一行代码即可在您的应用中启动并运行,以捕获未处理的异常。

使用BugSense,您还可以添加"面包屑"这些都会添加到您的错误报告中。然后,您可以从自己打开的服务位置进行检查,并添加此信息以帮助您从异常堆栈跟踪中找出问题。

答案 1 :(得分:0)

问题是当关闭Location时,它会抛出一个你没有在try / catch块中处理的异常。这是来自MSDN的示例代码来正确处理这个:

private async void OneShotLocation_Click(object sender, RoutedEventArgs e)
{

    if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] != true)
    {
        // The user has opted out of Location.
        return;
    }

    Geolocator geolocator = new Geolocator();
    geolocator.DesiredAccuracyInMeters = 50;

    try
    {
        Geoposition geoposition = await geolocator.GetGeopositionAsync(
            maximumAge: TimeSpan.FromMinutes(5),
            timeout: TimeSpan.FromSeconds(10)
            );

        LatitudeTextBlock.Text = geoposition.Coordinate.Latitude.ToString("0.00");
        LongitudeTextBlock.Text = geoposition.Coordinate.Longitude.ToString("0.00");
    }
    catch (Exception ex)
    {
        if ((uint)ex.HResult == 0x80004004)
        {
            // the application does not have the right capability or the location master   switch is off
            StatusTextBlock.Text = "location  is disabled in phone settings.";
        }
        //else
        {
            // something else happened acquring the location
        }
    }
}

您应该访问Source MSDN文章并阅读它以便更好地理解。该样本位于第7步。