【问题标题】:How to obtain the values of a method async c# windows phone 8.1如何获取方法异步 c# windows phone 8.1 的值
【发布时间】:2016-01-11 15:34:13
【问题描述】:

我有以下代码

 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");
        }
    }

但是我需要在没有异步方法返回值的情况下在相同的方法中执行此操作,我当然会以某种方式告诉我所有异步它都停止获取该值。

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

谢谢

【问题讨论】:

  • 您在等待 getLocation1() 吗?
  • 我需要实时获取纬度和经度的值作为 json 上的打印,但总是将我拉为 null,因为异步方法稍后发送值

标签: c# windows windows-phone-8.1 windows-8.1


【解决方案1】:

好的,首先你需要知道你不应该使用 async void。 它是一种允许您在其中使用 await 的结构,但是如果您 await getLocation1() 您的程序将不会等待它。 相反,您应该尽可能使用异步任务。因此,请尝试将 getLocation1 更改为 async Task,然后,当您等待它时,您将确定其中已完成的任务。

【讨论】:

  • 谢谢,您向我指出的解决方案是将方法更改为 async Task GetLocation ()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-18
  • 1970-01-01
相关资源
最近更新 更多