public WeatherHourlyItemViewModel(WeatherHourlyInfo info)
 {
     //RainInfo = $"降水 {info?.Precip}mm 率 {info?.Pop}";
     //TemperatureInfo = $"气温 {info?.Temp}℃";
     Title = $"{info?.FxTime.Hour}时 气温 {info?.Temp}℃";
     if (info?.Precip != 0 || info?.Pop != 0)
     {
         Title += $"\r\n降水 {info?.Precip} 率 {info?.Pop}";
     }
 }
Exemple #2
0
        private async Task <List <WeatherHourlyInfo> > GetHoursWeather()
        {
            if (id == null)
            {
                await RefreshLocationId();
            }
            string uri = "https://devapi.qweather.com/v7/weather/24h?location={0}&key={1}";

            uri = string.Format(uri, id, Key);

            HttpClientHandler handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            };
            HttpClient httpClient = new HttpClient(handler)
            {
                Timeout = TimeSpan.FromSeconds(3)
            };
            HttpResponseMessage response = await httpClient.GetAsync(uri);

            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();

            JObject responseJson = JsonConvert.DeserializeObject(responseBody) as JObject;
            int     code         = (int)responseJson?["code"];

            if (code != 200)
            {
                Exception e = new Exception($"天气预报获取错误 \r\n uri = {uri} \r\n code = {code}");
                throw e;
            }

            JArray hours = responseJson?["hourly"] as JArray;
            List <WeatherHourlyInfo> result = new List <WeatherHourlyInfo>();

            if (hours == null)
            {
                Exception e = new Exception("天气预报获取错误 \r\n uri = {uri} \r\n " + nameof(hours) + " == null");
                throw e;
            }
            foreach (JToken hour in hours)
            {
                WeatherHourlyInfo info = new WeatherHourlyInfo
                {
                    Temp   = (int)hour["temp"],
                    Precip = (float)hour["precip"],
                    Pop    = (int)hour["pop"],
                    FxTime = (DateTime)hour["fxTime"],
                };
                result.Add(info);
            }

            return(result);
        }
Exemple #3
0
        private List <WeatherHourlyItemViewModel> WeatherHourlyDemo()
        {
            WeatherHourlyInfo dailyInfo = new WeatherHourlyInfo
            {
            };
            WeatherHourlyItemViewModel vm = new WeatherHourlyItemViewModel(dailyInfo);

            List <WeatherHourlyItemViewModel> vms = new List <WeatherHourlyItemViewModel>();

            vms.Add(vm);
            vms.Add(vm);
            vms.Add(vm);
            vms.Add(vm);

            return(vms);
        }