I've created a <a href="https://msdn.microsoft.com/en-us/library/windows/apps/mt609012.aspx" rel="nofollow">Windows Runtime Component (C#)</a> in my javascript UWP app, but am having trouble getting it to return the result I want. It builds fine, but on debugging, null is returned, instead of the Geoposition object.
Since the function below needs to be called in my javascript app, it seems I <a href="https://marcominerva.wordpress.com/...async-methods-in-a-windows-runtime-component/" rel="nofollow">can't use the async keyword,</a> but I can do it in a wrapper:
Wrapper:
Javascript function call:
The reason why I need to do this part in C# is because one of the function calls I need is missing (perhaps the developers forgot to include it) from the javascript API, but exists in C#.
Since the function below needs to be called in my javascript app, it seems I <a href="https://marcominerva.wordpress.com/...async-methods-in-a-windows-runtime-component/" rel="nofollow">can't use the async keyword,</a> but I can do it in a wrapper:
Code:
public static Geoposition GetGeolocation()
{
Geolocator _geolocator = new Geolocator();
MapLocation mapInfo = null;
_geolocator.DesiredAccuracyInMeters = 5;
try
{
Geoposition currentPosition = GetGeopoisitionWrapper(_geolocator).Result;
// Geoposition currentPosition = GetGeopoisitionWrapper(_geolocator).AsAsyncOperation().GetResults();
//other stuff needing currentPosition, and another async function call
return currentPosition;
}
catch (Exception ex)
{
return null;
}
}
Wrapper:
Code:
private static async Task<Geoposition> GetGeopositionWrapper(Geolocator g)
{
Geoposition currentPosition = await g.GetGeopositionAsync(); // get the raw geoposition data
return currentPosition;
}
Javascript function call:
Code:
function getLocationForMap() {
Windows.Devices.Geolocation.Geolocator.requestAccessAsync().done(
function (accessStatus) {
switch (accessStatus) {
case Windows.Devices.Geolocation.GeolocationAccessStatus.allowed:
var result = SampleComponent.Example.getGeolocation();
break;
case Windows.Devices.Geolocation.GeolocationAccessStatus.denied:
WinJS.log && WinJS.log("Access to location is denied.", "sample", "error");
break;
case Windows.Devices.Geolocation.GeolocationAccessStatus.unspecified:
WinJS.log && WinJS.log("Unspecified error!", "sample", "error");
break;
}
},
function (err) {
WinJS.log && WinJS.log(err, "sample", "error");
});
}
The reason why I need to do this part in C# is because one of the function calls I need is missing (perhaps the developers forgot to include it) from the javascript API, but exists in C#.