c# - Task Position does not contain a definition Xamarin forms -
hello working on app in xamrian forms app needs ask gelocation permission , if granted needs geolocation data device , put geolocation coordinates forecast.io url using geolocator plugin james montemagno permissionsplugin james montemagno getting same error 2 times:
severity code description project file line suppression state error cs1061 'task' not contain definition 'longitude' , no extension method 'longitude' accepting first argument of type 'task' found (are missing using directive or assembly reference?) readymo c:\users\debroc1\documents\visual studio 2015\projects\app mac\appname\appname\appname\app\radarhome.xaml.cs 74 active
i can not run app because of these 2 errors here's forms code:
using xamarin.forms; using system; using system.diagnostics; using plugin.geolocator; namespace appname.radar { public interface mylocationtracker { void obtainmylocation(); event eventhandler<mylocationeventargs> locationobtained; } public interface mylocationeventargs { double lat { get; set; } double lng { get; set; } } public partial class radarhome : contentpage { public radarhome() { mylocationtracker msi; double betalat; double betalog; var locator = crossgeolocator.current; if (locator.isgeolocationenabled == false) { if (device.os == targetplatform.android) { msi.locationobtained += (object esender, mylocationeventargs ew) => { debug.writeline(ew.lat); }; msi.obtainmylocation(); } else if (device.os == targetplatform.ios) { msi = dependencyservice.get<mylocationtracker>(); msi.locationobtained += (object jsender, mylocationeventargs je) => { debug.writeline(je.lat); }; msi.obtainmylocation(); } } locator.desiredaccuracy = 50; var position = locator.getpositionasync(timeoutmilliseconds: 100000); betalat = position.latitude; //error here betalog = position.longitude; // same error here string str = string.format("https://forecast.io/?mobile=1#/f/lat:{0} , long: {1}", betalat, betalog); var client = new system.net.http.httpclient(); client.baseaddress = new uri(str); } } }
any ideas on i'm missing?
task unit of asynchronous work may ran in different thread. c# allows "await" task finish executing , unwrap result. avoid using .result @ cost lock current thread. more comprehensive explanation stephen cleary here.
public partial class radarhome : contentpage { private readonly crossgeolocator _locator; private double betalat; private double betalog; public radarhome() { mylocationtracker msi; _locator = crossgeolocator.current; if (_locator.isgeolocationenabled == false) { if (device.os == targetplatform.android) { msi.locationobtained += (object esender, mylocationeventargs ew) => { debug.writeline(ew.lat); }; msi.obtainmylocation(); } else if (device.os == targetplatform.ios) { msi = dependencyservice.get<mylocationtracker>(); msi.locationobtained += (object jsender, mylocationeventargs je) => { debug.writeline(je.lat); }; msi.obtainmylocation(); } } _locator.desiredaccuracy = 50; getpositionasynchronously(); string str = string.format("https://forecast.io/?mobile=1#/f/lat:{0} , long: {1}", betalat, betalog); var client = new system.net.http.httpclient(); client.baseaddress = new uri(str); } private async void getpositionasynchronously() { //will run asynchronously in diff thread var position = await _locator.getpositionasync(timeoutmilliseconds: 100000); betalat = position.latitude; //will work betalog = position.longitude; // work } }
Comments
Post a Comment