我有一个使用GeoLocator查找用户位置的应用程序。看起来像这样:public async (not sure what to put here) Cordinates(){ if (_geolocator == null) { _geolocator = new Geolocator(); } Geoposition _Position = await locator.GetGeopositionAsync();}我还有一个字符串属性,我想用它来描述过程。public string Status {get; set}例如,当Cordinates()开始时,字符串值应为“正在搜索...”,而当Cordinates()完成时,它可能会显示“完成”。我认为在这种情况下,我应该使用await / async,但请提供一些帮助,以解决问题。 最佳答案 如果您返回内容,则应将返回类型设置为Task<T>,否则则将返回类型设置为Task(void)。例如,如果您有类似return _position;的行,则应将函数的返回类型设置为Task<Geoposition>;如果具有return 0,则应将其更改为Task<int>,并且如果您不返回任何内容或只是只需使用return。public async Task<your return type> or Task if return type is void Cordinates(){ if (_geolocator == null) { _geolocator = new Geolocator(); } this.Status = "Searching..."; // seting status and awaiting your async GetGeoposition Geoposition _Position = await locator.GetGeopositionAsync(); this.Status = "Done"; // after GetGeoposition is finidshed you are here and setting status to done}
07-24 17:27
查看更多