Я пытаюсь сделать приложение погоды для города. Я создал класс, который может анализировать файл json, отправленный API openweathermap. Вначале я столкнулся с проблемой в модели представления, где ошибка о "WeatherModel. Root '- это тип, который недопустим в данном контексте."
I Я немного запутался, как заставить строку работать в viewmodel внутри функции LoadWeather (), где я пытаюсь дать выходную переменную класс WeatherModel. Root из переменной items, но он не работает. Вот модель Viewmoled:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using FFImageLoading.Forms;
using Newtonsoft.Json;
using System.IO;
using Xamarin.Forms;
namespace Weather_App_2
{
public class ViewModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name=null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public static (string, string, double) LoadWeather()
{
using (WebClient web = new WebClient())
{
string json = new WebClient().DownloadString("api.openweathermap.org/data/2.5/weather?id=2147714&appid=67f9bc5970b49b875ccecec84f849300units=metric&cnt=6");
var items = JsonConvert.DeserializeObject<WeatherModel>(json);
WeatherModel.Root output = items.Root;
cityName = string.Format($"{output.name}");
country = string.Format($"{output.sys.country}");
temp = output.main.temp - 276;
return (cityName, country, temp);
}
}
private static (string, string, double) values = LoadWeather();
public static string cityName = values.Item1;
public static string country = values.Item2;
public static double temp = values.Item3;
public string CityName
{
get { return cityName; }
set
{
cityName = value;
OnPropertyChanged();
}
}
public string Country
{
get { return country; }
set
{
country = value;
OnPropertyChanged();
}
}
public double Temp
{
get { return temp; }
set
{
temp = value;
OnPropertyChanged();
}
}
}
}
А вот класс WeatherModel:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Weather_App_2
{
public class WeatherModel
{
public class coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
}
public class main
{
public double temp { get; set; }
public double temp_min {get; set;}
public double temp_max { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
}
public class wind
{
public double speed { get; set; }
}
public class sys
{
public string country { get; set; }
}
public class Root
{
public coord coord { get; set; }
public List<weather> weather { get; set; }
public main main { get; set; }
public wind wind { get; set; }
public sys sys { get; set; }
public string country {get; set;}
public string name { get; set; }
}
}
}