public void GetWeather()
 {
     try
     {
         WeatherWSSoapClient client = new WeatherWSSoapClient();
         client.getWeatherCompleted += new EventHandler<getWeatherCompletedEventArgs>(client_getWeatherCompleted);
         client.getWeatherAsync("济南", "");
     }
     catch { }
 }
Exemplo n.º 2
0
        /*
            <GeocoderSearchResponse>
              <status>OK</status>
              <result>
                <location>
                  <lat>-71.988531</lat>
                  <lng>47.669444</lng>
                </location>
                <formatted_address></formatted_address>
                <business></business>
                <addressComponent>
                  <streetNumber></streetNumber>
                  <street></street>
                  <district></district>
                  <city></city>
                  <province></province>
                </addressComponent>
                <cityCode>0</cityCode>
              </result>
            </GeocoderSearchResponse>
         */

        protected void GetWeatherInfoHandler(double latitude, double longitude)
        {
            string url = String.Format(GetCityNameUrl, latitude, longitude, Guid.NewGuid().ToString("n"));
            Debug.WriteLine("Longitude:{0}, Latitude:{1}", longitude, latitude);

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method = "Get";

            Func<IObservable<WebResponse>> func = Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse);
            var result = func();
            result.ObserveOnDispatcher().Subscribe(response =>
            {
                Stream stream = response.GetResponseStream();
                if (null != stream)
                {
                    XDocument root = XDocument.Load(stream);
                    var cityInfo = from city in root.Descendants("addressComponent")
                                   select new
                                   {
                                       CityName = city.Element("city").Value,
                                       Province = city.Element("province").Value
                                   };

                    tempCityName = cityInfo.FirstOrDefault().CityName.TrimEnd('市');
                    Province = cityInfo.FirstOrDefault().Province.TrimEnd('省').TrimEnd('市');

                    if (String.IsNullOrEmpty(tempCityName.Trim()))
                    {
                        this.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        WeatherWSSoapClient client = new WeatherWSSoapClient();
                        var obWeather = Observable.FromEvent<getWeatherCompletedEventArgs>(client, "getWeatherCompleted").Select(weather => weather.EventArgs);
                        var obProvince = Observable.FromEvent<getRegionProvinceCompletedEventArgs>(client, "getRegionProvinceCompleted").Select(p => p.EventArgs);
                        var obCity = Observable.FromEvent<getSupportCityStringCompletedEventArgs>(client, "getSupportCityStringCompleted").Select(p => p.EventArgs);

                        obProvince.ObserveOnDispatcher().Subscribe(p =>
                        {
                            if (null == provinces)
                                provinces = new Dictionary<string, string>();

                            foreach (string info in p.Result)
                            {
                                string[] arr = info.Split(new string[] { "," }, StringSplitOptions.None);
                                provinces.Add(arr[0], arr[1]);
                            }

                            if (provinces.ContainsKey(Province))
                            {
                                client.getSupportCityStringAsync(provinces[Province]);
                            }
                        });

                        obCity.ObserveOnDispatcher().Subscribe(city =>
                        {
                            var ci = from c in city.Result
                                     where c.Split(new string[] { "," }, StringSplitOptions.None)[0].Equals(tempCityName)
                                     select c.Split(new string[] { "," }, StringSplitOptions.None)[1];

                            client.getWeatherAsync(ci.FirstOrDefault(), String.Empty);
                        });

                        obWeather.ObserveOnDispatcher().Subscribe(weather =>
                        {
                            if (null == weather || null != weather.Error)
                            {
                                this.Visibility = Visibility.Collapsed;
                            }

                            Temperature = weather.Result[8];
                            WeatherInfo = weather.Result[7].Split(new string[] { " " }, StringSplitOptions.None)[1];
                            WeatherIconStart = weather.Result[10];
                            WeatherIconEnd = weather.Result[11];
                            CityName = tempCityName;

                            ShownAnimation();
                        },
                        exception =>
                        {
                            this.Visibility = Visibility.Collapsed;
                        });

                        if (null == provinces)
                        {
                            client.getRegionProvinceAsync();
                        }
                        else
                        {
                            if (provinces.ContainsKey(Province))
                            {
                                client.getSupportCityStringAsync(provinces[Province]);
                            }
                        }
                    }
                }
            },
                exception =>
                {
                    this.Visibility = Visibility.Collapsed;
                });
        }