How to solve System exception on button click while trying to access location?

1

I am new to visual studio and developing universal windows applications. My program throws an exception when I click a UI button that should display location information on click. It is a UWP. The location access is turned on from the package manifest. The code looks like this:

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);
        }


    }    

The exception stacktrace is as below:

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.TaskAwaiter1.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.TaskAwaiter1.GetResult() at HelloLocation.MainPage.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.TaskAwaiter1.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.TaskAwaiter1.GetResult() at HelloLocation.MainPage.d__1.MoveNext() The program '[3032] hellolocation.exe' has exited with code -1 (0xffffffff).

c#
exception
visual-studio-2015
async-await
iot
asked on Stack Overflow Oct 5, 2016 by nj_bubbles • edited Oct 5, 2016 by Uwe Keim

2 Answers

0

You need to first request access, and then use the result from this request to determine if you can attempt to use the GetGeopositionAsync call.

Details 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;
}
answered on Stack Overflow Oct 5, 2016 by David Pine
0

It works now. :) I had been working with a visual studio running on a virtual machine earlier. I guess it doesn't have the required permission to access location even though location was enabled in both real (my PC) and virtual machine. I used the same code with another laptop directly without a virtual machine and it works perfectly well !

answered on Stack Overflow Oct 5, 2016 by nj_bubbles

User contributions licensed under CC BY-SA 3.0