I'm working with Xamarin.Forms, I have a method in one of my ViewModel that searches a web service for a list of Objects and later these objects are displayed in a View, so far everything works normally.
The problem is as follows, I want to know how do I display to the user a message if the return of my method is an empty list?
public async Task GetLista()
{
aguarde = true;
HttpClient cliente = new HttpClient();
cliente.BaseAddress = new Uri("URLDOMEUWEBSERVICE");
cliente.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string url = $"BuscaDestino/{id1}/{id2}";
var resultado = await cliente.GetStringAsync(url);
var buscaJson = JsonConvert.DeserializeObject<BuscaJson[]>(resultado);
if (resultado != null)
{
foreach (var destinos in buscaJson)
{
this.Lista.Add(new Destinos
{
Campo1 = destinos.campo1,
Campo2 = destinos.campo2,
Campo3 = destinos.campo3
});
}
aguarde = false;
}
else
{
MessagingCenter.Send<CidadeDestino>(cidade, "FalhaNalistagem");
aguarde = false;
}
CodeBehind from my View:
protected async override void OnAppearing()
{
base.OnAppearing();
//chama o método que busca a lista
await this.ViewModel.GetLista();
MessagingCenter.Subscribe<CidadeDestino>(this, "FalhaNalistagem",
async (cidade)=>
{
await DisplayAlert("Erro", "Não Foram Localizados resultados! Tente Novamente", "ok");
});
}
My idea was if the "result" of the search was null sends a MessagingCenter to the codebehind and would display a DisplayAlert with a msg, but it did not work out what I tried. Thank you in advance for anyone who can help.