1
Guys as I load a Listview with the result of a method that returns an object already loaded with JSON?
Method that loads object with JSON
namespace MeuProjeto.ViewModels.jsonModels
{
class JSON
{
/*async void getJSON<T>(object sender, String URL, System.EventArgs e)*/
public async Task<T> getJSON<T>(String URL, Boolean Complemento, Boolean UtilizarExemplo)
{
var response = @"{'avisos':[
{
""id"": 1,
""titulo"": ""blá blá"",
""descricao"": ""Hoje terá comida"",
""data_inicial"": ""28/10/2017"",
""data_final"": ""28/10/2017""
},
{
""id"": 2,
""titulo"": ""Atraso"",
""descricao"": ""Esta semana irei atrasar 10minutos"",
""data_inicial"": ""30/10/2017"",
""data_final"": ""03/11/2017""
}
]}";
var client = new HttpClient();
/*var dados = JsonConvert.DeserializeObject<UsuarioList>(response); */
if (UtilizarExemplo)
{
T dados = JsonConvert.DeserializeObject<T>(response);
return dados;
}
else
{
var json = await client.GetStringAsync(URL);
if (Complemento)
json = "{" + Complemento + ":" + json + "}";
T dados = JsonConvert.DeserializeObject<T>(json);
return dados;
}
}
}
How I call the method
await json.getJSON<ListaAviso>("", true, true);
And my model Listview
namespace MeuProjeto.ViewModels
{
public class ViewList : ViewModel
{
public ICommand LoadCommand { get; set; }
public ObservableCollection<Item> Items { get; set; }
private int _index = 0;
public virtual void Load(Boolean lCarregarVarios = true) { }
public ViewList()
{
this.Items = new ObservableCollection<Item>();
LoadCommand = new Command(() => Load(true));
Load(false);
}
}
public class Item : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
string id;
public string ID
{
get { return id; }
set
{
text = value;
this.Notify("ID");
}
}
string descricao;
public string Descricao
{
get { return descricao; }
set
{
detail = value;
this.Notify("Descricao");
}
}
[...] Demais campos [...]
public override string ToString() { return this.Text; }
private void Notify(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Thank you so much for the help little guy helped a lot, seeing your example I managed to apply in what I need.
– Matheus Ribeiro