1
I am working with Xamarin.Forms, I have a method in one of my Viewmodel that searches in a web service a list of Objects and later these objects are displayed in a View, so far everything works normally. The problem is this, I want to know how I display to the user a message if the return of my method is an empty list? I’ll show the Viewmodel:
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 of 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 an msg, but it didn’t work out what I tried. Thank you in advance to those who can help.