Search code examples
c#listxamarin.formsbindinghttpclient

Xamarin Forms - How to display data binded (List from HttpClient)?


Can't display data in list view after json deserialization. How do i need to set it up in order to see it?

I'm data binding my reponse from http client using binding context so i can use it in my list view.

MainPage.xaml

<ContentPage
        Title="Home"
     >
        <Grid>
            <ListView ItemsSource="{Binding .}">
            <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Label Text="{Binding Description}"/>
                            <Label Text="{Binding Value}"/>
                        </StackLayout>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </ContentPage>

MainPage.cs

public MainPage()
        {
            DataService ds = new DataService();

            BindingContext = ds.GetBillsAsync();

            InitializeComponent();

            Chart1.Chart = new BarChart { Entries = entries, LabelTextSize = (float)Convert.ToDouble(Device.GetNamedSize(NamedSize.Large, typeof(Label))), BackgroundColor = SKColors.Transparent };



        }

DataService.cs

public class DataService
    {
        HttpClient client = new HttpClient();

        public async Task<List<Bill>> GetBillsAsync()
        {
            try
            {
                string url = "my url";

                var response = await client.GetStringAsync(url).ConfigureAwait(continueOnCapturedContext: false); ;
                var bills = JsonConvert.DeserializeObject<List<Bill>>(response);
                return bills;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }

I'm not getting any error messages, except for this output message "Binding: System.Threading.Tasks.Task`1[System.Collections.Generic.List`1[Test.Models.Bill]] can not be converted to type 'System.Collections.IEnumerable'" which i don't think it is the problem since i can see my list being populated correctly when i'm debugging.

Can you help me please?

Thank you in advance


Solution

  • Nikhil's method is one way ,if you only want to set the List which request from http client to the listview,you could binding to the ListView's ItemsSource property.

    in the MainPage.xaml.cs :

    public partial class MainPage: ContentPage
    {
       DataService ds = new DataService();
       public MainPage()
        {
    
            InitializeComponent();
            Chart1.Chart = new BarChart { Entries = entries, LabelTextSize = (float)Convert.ToDouble(Device.GetNamedSize(NamedSize.Large, typeof(Label))), BackgroundColor = SKColors.Transparent };
        }
    
       protected override async void OnAppearing()
        {
            base.OnAppearing();
            listview.ItemsSource = await ds.GetBillsAsync();
        }
    }
    

    in MianPage.xaml:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             Title="Home"
             x:Class="App18.MainPage">
      <ContentPage.Content>
        <StackLayout>
            <ListView x:Name="listview">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Label Text="{Binding Description}"/>
                            <Label Text="{Binding Value}"/>
                        </StackLayout>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
     </ContentPage.Content>
    </ContentPage>