【问题标题】:Windows 8 phone app crashes on open when location services are off位置服务关闭时,Windows 8 手机应用程序在打开时崩溃
【发布时间】:2014-07-09 08:43:06
【问题描述】:

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

感谢您的帮助:)

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      问题是当 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 步。

      【讨论】:

        猜你喜欢
        • 2015-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 2022-11-04
        • 1970-01-01
        • 2013-07-19
        • 2011-07-16
        相关资源
        最近更新 更多