从PCL中的异步调用返回值

时间:2015-04-01 22:05:47

标签: c# xamarin.forms

我有一个非PCL方法,它从RESTful服务中获取数据,然后对其进行反序列化并返回数据。看起来像这样

    public WeatherData GetWeather
    {
        get
        {
            if (!ftrackData.Online)
                return new WeatherData();

            string param = string.Format("lat={0}&lon={1}&APPID={2}", AppDelegate.Self.Latitude, AppDelegate.Self.Longitude, AppDelegate.Self.WeatherID);
            var request = WebRequest.Create("http://api.openweathermap.org/data/2.5/forecast?" + param) as HttpWebRequest;
            request.Method = "GET";
            request.Accept = "application/json";
            request.ContentType = "application/json";

            string responseContent;
            try
            {
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        responseContent = reader.ReadToEnd();
                    }
                }
                var deserial = Deserialize<WeatherData>(responseContent);
                return deserial;
            }
            catch (WebException ex)
            {
                Console.WriteLine("Deserialiser failed : {0}--{1}", ex.StackTrace, ex.Message);
                return null;
            }
        }
    }

(数据来自openweathermap)

我已将其移至我的PCL并提出以下

    public WeatherData GetWeather
    {
        get
        {
            var param = string.Format("lat={0}&lon={1}&APPID={2}", App.Self.Latitude, App.Self.Longitude, App.Self.WeatherID);
            var request = WebRequest.Create("http://api.openweathermap.org/data/2.5/forecast?" + param) as HttpWebRequest;
            request.Method = "GET";
            request.Accept = "application/json";
            request.ContentType = "application/json";

            var Weather = new WeatherData();
            string responseContent;
            try
            {
                var asyncResult = request.BeginGetResponse(new AsyncCallback(s =>
                        {
                            var response = (s.AsyncState as WebRequest).EndGetResponse(s);

                            using (var reader = new StreamReader(response.GetResponseStream()))
                            {
                                responseContent = reader.ReadToEnd();
                            }
                            Weather = Deserialize<WeatherData>(responseContent);
                        }), request);
                return Weather;

            }
            catch (WebException ex)
            {
                Debug.WriteLine("Deserialiser failed : {0}--{1}", ex.StackTrace, ex.Message);
                return Weather;
            }
        }
    }

代码构建没有问题,但在返回天气数据之前不等待。

我已尝试将.AsyncWaitHandle.WaitOne()添加到BeginGetResponse的末尾,但在通话完成之前仍会返回Weather对象。

当回调返回void时,无法将回复添加到匿名回调中。

我也尝试过添加

if (asyncResult.IsCompleted)
   return Weather;

但是这不会编译,因为并非所有路径都返回一个值。

如果在匿名回电中填写天气之前,如何才能获得返回?

0 个答案:

没有答案