当定位服务关闭时,Windows 8手机应用程序会崩溃

本文关键字:手机 应用程序 崩溃 Windows 服务 定位 | 更新日期: 2023-09-27 18:12:17

我有一个天气应用程序,它以纬度和经度获取用户位置,然后使用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));
        }

谢谢你的帮助:)

当定位服务关闭时,Windows 8手机应用程序会崩溃

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

堆栈跟踪显示可能有延迟。BugSense是一个非常有用的工具,它将为您提供大量的错误报告,以帮助调试。只需要一行代码就可以在应用程序中启动和运行,以捕获未处理的异常。

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

问题是,当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
        }
    }
}

您应该访问源MSDN文章并阅读它以更好地理解。这个示例是在步骤7。