Forum Cross Platform with Xamarin

How i can convert a Task<List<Model>> result from webservice to an ObservableCollection?

Hello to everyone,

I get a data from webservice, but when I pass the Task<List> to the listview binding, i get an error of converting: "You can not implicitly convert the type system.threading.task.task<system.collections.generic.list> to system.collections.generic.ienumerable"

I was searching but i cant find the code to convert this.

Can anyone help me?



this is my method that connect to the webservice, work fine:

public async Task<List<Casos>> ConsultarCasosWebService(int id_cliente, string token)

            clienteHttp = new HttpClient();
            var direURL = ""+ id_cliente +"/?api_token="+ token;
            var uri = new Uri(string.Format(direURL, string.Empty));

            var response = await clienteHttp.GetAsync(uri);
            if (response.IsSuccessStatusCode)
                var content = await response.Content.ReadAsStringAsync();
                cases = JsonConvert.DeserializeObject<List<Casos>>(content);

            return cases;

and this is part of where i need to bind the itemsource of my listview, dont work.

casosService = new CargarCasosService();
            //casos = new ObservableCollection<Casos>(casosService.ConsultarCasos());
            casosWeb = new ObservableCollection<Casos>(casosService.ConsultarCasosWebService(1, "token=="));

            //transgormando el task a ienumerable
            IEnumerable<Casos> casosw = casosWeb;

            //cargando la lista de la vista y creando el evento de seleccion
            lstCases.ItemsSource = casosweb;
            lstCases.ItemSelected += LstCases_ItemSelected;

Best Answer


  • juanzeta33juanzeta33 USMember ✭✭

    hey @JohnMiller , thank you so much, this work fine to me.

Sign In or Register to comment.