[英]Xamarin Forms - How to display data binded (List from HttpClient)?
json 反序列化后無法在列表視圖中顯示數據。 我需要如何設置才能看到它?
我正在使用綁定上下文從 http 客戶端對我的響應進行數據綁定,因此我可以在我的列表視圖中使用它。
主頁.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>
主頁.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 };
}
數據服務.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;
}
}
}
我沒有收到任何錯誤消息,除了此 output 消息"Binding: System.Threading.Tasks.Task`1[System.Collections.Generic.List`1[Test.Models.Bill]] can not be converted to type 'System.Collections.IEnumerable'"
我認為這不是問題,因為我在調試時可以看到我的列表被正確填充。
你能幫我嗎?
先感謝您
Nikhil 的方法是一種方法,如果您只想將來自 http 客戶端的請求的列表設置到列表視圖,您可以綁定到列表視圖的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>
您需要更改 GetBillsAsync 方法以檢索 Array,然后使用 Array.ToList() 和 System.Linq 轉換為 List。
您需要為此實現一個 ViewModel。 您可以將 NuGet MVVM 幫助程序用於 BaseViewModel 或實現您自己的。
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()
}
}
}
然后您的 MainPage 變為:-
HomeViewModel 虛擬機; 公共主頁(){
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}">
這些更改應該有效。 如果您遇到困難,請告訴我。 謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.