User Alert Message - Xamarin.Forms

1

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.

    
asked by anonymous 21.06.2018 / 14:51

1 answer

0

The problem was solved, I just changed the approach, in ViewModel I added one:

if(this.ListaDestinos.Count == 0)
            {
              MessagingCenter.Send<CidadeDestino>(cidade, "FalhaNalistagem");
            }

If the list is empty (.count == 0), it sends the MessagingCenter to View and it displays the response. Just that.

    
21.06.2018 / 16:14