在尝试访问位置时如何解决按钮单击时的系统异常?

时间:2016-10-05 11:22:22

标签: c# exception visual-studio-2015 async-await iot

我是视觉工作室的新手并开发通用的Windows应用程序。当我单击应该在点击时显示位置信息的UI按钮时,我的程序会抛出异常。这是一个UWP。从包清单打开位置访问权限。 代码如下所示:

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

        try
        {
            var geoLocator = new Geolocator();
            geoLocator.DesiredAccuracy = PositionAccuracy.High;
            Geoposition positionInfo = await geoLocator.GetGeopositionAsync();
            string latitude = "Latitude: " + positionInfo.Coordinate.Point.Position.Latitude.ToString();
            string longitude = "Longitude: " + positionInfo.Coordinate.Point.Position.Longitude.ToString();
        }
        catch (Exception ex)
        {

            Debug.WriteLine(ex.Message);
            Debug.WriteLine(ex.StackTrace);
        }


    }    

异常堆栈跟踪如下:

  

抛出异常:&#39; System.Exception&#39;在mscorlib.ni.dll中   来自HRESULT的异常:0x80072EE7      在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at HelloLocation.MainPage.<getBtn_Click>d__1.MoveNext() Exception thrown: 'System.Exception' in mscorlib.ni.dll Exception from HRESULT: 0x80072EE7 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()      在HelloLocation.MainPage.d__1.MoveNext()   抛出异常:&#39; System.Exception&#39;在mscorlib.ni.dll中   来自HRESULT的异常:0x80072EE7      在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at HelloLocation.MainPage.<getBtn_Click>d__1.MoveNext() Exception thrown: 'System.Exception' in mscorlib.ni.dll Exception from HRESULT: 0x80072EE7 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()      在HelloLocation.MainPage.d__1.MoveNext()   程序&#39; [3032] hellolocation.exe&#39;退出时使用代码-1(0xffffffff)。

2 个答案:

答案 0 :(得分:0)

您需要先请求访问权限,然后使用此请求的结果来确定您是否可以尝试使用GetGeopositionAsync来电。

详情here

using Windows.Devices.Geolocation;
...
var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus) {
 case GeolocationAccessStatus.Allowed:

  // If DesiredAccuracy or DesiredAccuracyInMeters are not set (or value is 0), DesiredAccuracy.Default is used.
  Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = _desireAccuracyInMetersValue };

  // Subscribe to StatusChanged event to get updates of location status changes
  _geolocator.StatusChanged += OnStatusChanged;

  try {                        
   // Carry out the operation
   Geoposition pos = await geolocator.GetGeopositionAsync();
  } catch (Exception ex) {
   // handle the exception (notify user, etc.)
  }
  break;

 case GeolocationAccessStatus.Denied:
  // handle access denied case here
  break;

 case GeolocationAccessStatus.Unspecified:
  // handle unspecified error case here
  break;
}

答案 1 :(得分:0)

现在有效。 :)我之前一直在使用虚拟机上运行的visual studio。我猜它没有访问位置所需的权限,即使在真实(我的PC)和虚拟机中都启用了位置。 我在没有虚拟机的情况下直接在另一台笔记本电脑上使用了相同的代码,效果非常好!