2015-10-13 58 views
-1

我有以下代码如何获取的方法异步C#的windows phone的值8.1

async void getLocation1() 
    { 
     try { 

      var geolocator = new Geolocator(); 
      Geoposition position = await geolocator.GetGeopositionAsync(); 

      // reverse geocoding 
      BasicGeoposition myLocation = new BasicGeoposition 
      { 
       Longitude = position.Coordinate.Longitude, 
       Latitude = position.Coordinate.Latitude             
      };        

      Geopoint pointToReverseGeocode = new Geopoint(myLocation); 
      MapLocationFinderResult result = await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode); 

      // here also it should be checked if there result isn't null and what to do in such a case 

      PostalCode1 = result.Locations[0].Address.PostCode; 
      Country1 = result.Locations[0].Address.Country; 
      City1 = result.Locations[0].Address.Town; 
      State1 = result.Locations[0].Address.Region; 

      guardarLatit = myLocation.Latitude.ToString(); 
      guardarLong = myLocation.Longitude.ToString(); 

      await GeolocationWait(); 

      MessageDialog msgbox3 = new MessageDialog("Latitud: " + guardarLatit + "Longitud: " + guardarLong); 
      MessageDialog msgbox4 = new MessageDialog("PostalCode: " + PostalCode1 + " Country: " + Country1 + "City: " + City1 + "State: " + State1); 

      geolocation.Add("Latitud: " + guardarLatit); 
      geolocation.Add("Longitud: " + guardarLong); 
      geolocation.Add("PostalCode: " + PostalCode1); 
      geolocation.Add("Country: " + Country1); 
      geolocation.Add("City: " + City1); 
      geolocation.Add("State: " + State1); 

      await msgbox3.ShowAsync(); 
      await msgbox4.ShowAsync(); 

     } catch (Exception ex) 
     { 
      MessageDialog msgboxE = new MessageDialog("Error"); 
      await msgboxE.ShowAsync(); 
      geolocation.Add("Latitud: null"); 
      geolocation.Add("Longitud: null"); 
      geolocation.Add("PostalCode: null"); 
      geolocation.Add("Country: null"); 
      geolocation.Add("City: null"); 
      geolocation.Add("State: null"); 
     } 
    } 

,但我需要做的是在相同的方法,无需异步方法的返回值,我一定会通过某种途径告诉我所有的异步,它停止获取该值。

我的问题是,当我打印经度和纬度零时抛出我,因为异步方法发送的值肯定不是按时。

谢谢

+0

你在等待getLocation1()吗? – kernanb

+0

我需要实时获取纬度和经度的值作为json上的打印,但总是将我拉到空,因为异步方法稍后发送值 –

回答

0

好了,首先你要知道,你不应该使用异步无效。 这是一个允许你使用它的构造,但是如果你等待getLocation1()你的程序不会等待它。 相反,您应该在可能的情况下始终使用异步任务。因此,请尝试将getLocation1更改为异步任务,然后在您等待之后,确保其中已完成任务。

+0

谢谢,您向我指示的解决方案是将方法更改为异步任务GetLocation() –

+0

那么你能标记我的答案为...答案? ;) – Festyk