c# - Windows phone 8.1 GeoLocator GeoPositionAsync not working unless in code behind -


i spent several hours researching , trying various things solve apparently common issue in 8.1 sdk geolocator.getgeopositionasync call never returns or falls threw exception throws (per google search) ex.hresult == 0x80004004. after doing digging, noticed error silently hidden inside desiredaccuracyinmeters if not set.

even after this, way able work placing code inside code behind of windows phone view trying have call method inside button click. starting think there threading issue don't understand? i'm not great @ ui threads , that, think using proper usage of async , await follow way back.

here code. geolocator.getgeopositionasync method inside phone extension library created nd class looks this:

 public class placefinder : iplacefinderprovider<maproute> {     private dictionary<string, bool> settings = usersettingsprovider.getsetting<bool>(new list<string>() { "hasuserconsent" });      public async task<geolocationmodel> getuserlocationasync()     {         var geolocation = new geolocationmodel();          try         {              if (settings.any(x => x.key == "hasuserconsent" && x.value == true))             {                 geolocation.hasuserconsent = true;                 var locator = new geolocator                 {                     desiredaccuracyinmeters = 500,                     desiredaccuracy = positionaccuracy.high,                     movementthreshold = 1                 };                   var position = await locator.getgeopositionasync(timespan.fromminutes(5), timespan.fromseconds(10));                  geolocation.userslongitude = position.coordinate.longitude;                 geolocation.userslatitude = position.coordinate.latitude;              }             else             {                 throw new locationexception(locationexception.location_services_user_acceptance_failed);             }         }         catch (locationexception)         {             throw;         }         catch (unauthorizedaccessexception ex)         {             throw new locationexception(locationexception.location_services_unavailable);         }         catch (exception ex)         {             if ((uint)ex.hresult == 0x80004004)             {                 throw new locationexception(locationexception.location_services_turned_off);             }             throw ex;         }          return geolocation;     } 

then call use above in separate helper method:

 public class geolocationhelper {      public async static task<geolocationmodel> finduserlocation()     {         placefinder finder = new placefinder();         return await finder.getuserlocationasync();     } 

inside same code behind file,

this not work:

 location = await geolocationhelper.findlocation(); 

using code directly custom library works:

 placefinder finder = new placefinder();  var userlocation = await finder.getuserlocationasync(); 

so question is, why direct call library work , helper class cause never return?


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -