User alert message - Xamarin.Forms

Asked

Viewed 177 times

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.

1 answer

0

The problem was solved, I just changed the approach, in Viewmodel I added a :

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

If the list is empty (.Count == 0), send Messagingcenter to View and it displays the answer. That’s all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.