Xamarin Forms - 如何显示绑定的数据(来自 HttpClient 的列表)?

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

json 反序列化后无法在列表视图中显示数据。我需要如何设置才能看到它?

我正在使用绑定上下文对来自 http 客户端的响应进行数据绑定,因此我可以在我的列表视图中使用它。

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;
            }
        }
    }

我没有收到任何错误消息,除了这个输出消息 "Binding: System.Threading.Tasks.Task`1[System.Collections.Generic.List`1[Test.Models.Bill]] can not be converted to type 'System.Collections.IEnumerable'",我认为这不是问题,因为我在调试时可以看到我的列表被正确填充。

你能帮帮我吗?

提前致谢

您需要更改 GetBillsAsync 方法以检索数组,然后使用 Array.ToList() 和 System.Linq 转换为列表。

您需要为此实现一个 ViewModel。您可以为 BaseViewModel 使用 NuGet MVVM 帮助器或实现您自己的帮助器。

using System.Windows.Input;
using MvvmHelpers.Interfaces;

namespace NameSpace.ViewModel
{
    public partial class HomeViewModel : BaseViewModel
    {

        private ObservableCollection<Bill> _listOfbills = new ObservableCollection<Bill>();
        public ObservableCollection<Bill> ListOfbills
        {
            get => _listOfbills;
            set => SetProperty(ref _listOfbills, value);
        }
        public HomeViewModel()
        {
            DataService ds = new DataService();
            ListOfbills = ds.GetBillsAsync()
        }
    }
}

然后你的主页变成:-

HomeViewModel 虚拟机; public 主页() {

            BindingContext = vm = new HomeViewModel();

            InitializeComponent();

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



        }

然后在 Xaml 中,您可以按如下方式更改此行:-

<ListView ItemsSource="{Binding ListOfbills}">

这些更改应该有效。如果您遇到困难,请告诉我。谢谢!

Nikhil的方法是一种方法,如果你只想设置从http客户端请求到listview的List,你可以绑定到ListView的ItemsSource 属性 .

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();
    }
}

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>