1
votes

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).

2
Have you tried calling the method synchronously? Instead of using await x.GetGeoPositionAsync(), use: y = x.GetGeoPosition[Async]()[.Result]; See if that changes something.SimonC
@Beatsleigher I tried using the exact same code you suggested, but it throws an error : Error CS1061 'Geolocator' does not contain a definition for 'GetGeoPosition' and no extension method 'GetGeoPosition' accepting a first argument of type 'Geolocator' could be found (are you missing a using directive or an assembly reference?)nj_bubbles

2 Answers

0
votes

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;
}
0
votes

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 !