コード例 #1
0
        private async Task <List <WeatherDailyInfo> > GetDaysWeather(int dayNumber)
        {
            if (dayNumber != 3 && dayNumber != 7)
            {
                return(null);
            }
            if (id == null)
            {
                await RefreshLocationId();
            }
            string uri = "https://devapi.qweather.com/v7/weather/{2}d?location={0}&key={1}";

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

            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 days = responseJson?["daily"] as JArray;
            List <WeatherDailyInfo> result = new List <WeatherDailyInfo>();

            if (days == null)
            {
                Exception e = new Exception("天气预报获取错误 \r\n uri = {uri} \r\n " + nameof(days) + " == null");
                throw e;
            }
            foreach (JToken day in days)
            {
                WeatherDailyInfo info = new WeatherDailyInfo
                {
                    TempMax   = (int)day["tempMax"],
                    TempMin   = (int)day["tempMin"],
                    FxDate    = (DateTime)day["fxDate"],
                    IconDay   = (int)day["iconDay"],
                    TextDay   = (string)day["textDay"],
                    IconNight = (int)day["iconNight"],
                    TextNight = (string)day["textNight"]
                };
                result.Add(info);
            }

            return(result);
        }
コード例 #2
0
        public WeatherDailyItemViewModel(WeatherDailyInfo weatherDailyInfo)
        {
            IconDay         = weatherDailyInfo.IconDay;
            IconNight       = weatherDailyInfo.IconNight;
            TemperatureInfo = $"气温 {weatherDailyInfo?.TempMin} ~ {weatherDailyInfo?.TempMax}℃";

            int culturalWeek = ((int)DateTime.Today.DayOfWeek - 1 + 7) % 7;
            //使 thisWeekEnd 所指日期在 我国的 本周日
            DateTime thisWeekEnd = DateTime.Today.AddDays(6 - culturalWeek);

            var    weeks     = new string[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
            string dayOfWeek = weeks[(int)weatherDailyInfo.FxDate.DayOfWeek];

            if (weatherDailyInfo.FxDate > thisWeekEnd)
            {
                dayOfWeek = "下" + dayOfWeek;
            }
            Title = dayOfWeek + "天气预报";
        }
コード例 #3
0
        private List <WeatherDailyItemViewModel> WeatherDailyDemo()
        {
            WeatherDailyInfo dailyInfo = new WeatherDailyInfo
            {
                TempMax   = 10,
                TempMin   = 5,
                TextDay   = "晴",
                TextNight = "小雨",
                IconDay   = 100,
                IconNight = 150,
            };
            WeatherDailyItemViewModel vm = new WeatherDailyItemViewModel(dailyInfo);

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

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

            return(vms);
        }