Xamarin 中的 ListView 使用 Rest Api
ListView in Xamarin using RestApi
我能够解决这个错误。我想显示 ListView
的 API 数据,这些数据数量很大。
例如API包含这种类型的数据:
[{"id":"666","employee_name":"xyz","employee_salary":"123","employee_age":"23","profile_image":""}]
截图错误:
Class.cs 这是我在将 JSON 转换为 c#
之后制作的
public class employees
{
public string id { get; set; }
public string employee_name { get; set; }
public string employee_salary { get; set; }
public string employee_age { get; set; }
public string profile_image { get; set; }
}
这是 XAML.cs 文件,其中 LoadData()
用于调用 API
public async void LoadData()
{
var content = "";
HttpClient client = new HttpClient();
var RestURL = "MY API";
client.BaseAddress = new Uri(RestURL);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(RestURL);
content = await response.Content.ReadAsStringAsync();
var Items = JsonConvert.DeserializeObject<List<employees>>(content);
ListView1.ItemsSource = Items;
}
这是Xamarin.Forms的XAML文件:
<StackLayout BackgroundColor="White">
<ListView x:Name="ListView1" RowHeight="60">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" Padding="8,0,8,0">
<Label Text="{Binding id}" TextColor="#000" FontSize="14" LineBreakMode="TailTruncation" />
<Label Text="{Binding employee_name}" TextColor="#000" LineBreakMode="TailTruncation" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
首先需要创建一个本地模型class :
public class TodoItem
{
public string id { get; set; }
public string employee_name { get; set; }
public string employee_salary{ get; set; }
public bool employee_age { get; set; }
public bool profile_image { get; set; }
}
而在RestService
可以使用TodoItem
:
public List<TodoItem> Items { get; private set; }
public async Task<List<TodoItem>> RefreshDataAsync ()
{
Items = new List<TodoItem> ();
// RestUrl = http://developer.xamarin.com:8081/api/todoitems
var uri = new Uri (string.Format (Constants.RestUrl, string.Empty));
try {
var response = await client.GetAsync (uri);
if (response.IsSuccessStatusCode) {
var content = await response.Content.ReadAsStringAsync ();
Items = JsonConvert.DeserializeObject <List<TodoItem>> (content);
}
} catch (Exception ex) {
Debug.WriteLine (@"ERROR {0}", ex.Message);
}
return Items;
}
最后,您的列表视图 itemsource
可以设置如下:
listView.ItemsSource = await RestService.RefreshDataAsync();
注:这里有一个official sample可以参考
I'm enable to solve this error, i wanted to display listview of API data which is in large amount
在listview中显示大数据,这里有一个分页显示的方法。得到API数据后,保存在本地CacheListData
。不直接设置它到 listView.ItemsSource
。你需要创建一个 ItemListData
从 CacheListData.Which 添加数据 根据你曾经想要 show.When 列表视图滚动到底部,然后显示添加更多滑动方法以重新加载下一页数据。
一般来说,解决方案就是将大量数据缓存到本地first.Then获取数据一点点分页展示Here is a solution link can refer to.
您使用了错误的端点,您应该使用这个端点来检索员工列表
var RestURL = "http://dummy.restapiexample.com/api/v1/employees/";
您可以查看 API documentation
我能够解决这个错误。我想显示 ListView
的 API 数据,这些数据数量很大。
例如API包含这种类型的数据:
[{"id":"666","employee_name":"xyz","employee_salary":"123","employee_age":"23","profile_image":""}]
截图错误:
Class.cs 这是我在将 JSON 转换为 c#
之后制作的 public class employees
{
public string id { get; set; }
public string employee_name { get; set; }
public string employee_salary { get; set; }
public string employee_age { get; set; }
public string profile_image { get; set; }
}
这是 XAML.cs 文件,其中 LoadData()
用于调用 API
public async void LoadData()
{
var content = "";
HttpClient client = new HttpClient();
var RestURL = "MY API";
client.BaseAddress = new Uri(RestURL);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(RestURL);
content = await response.Content.ReadAsStringAsync();
var Items = JsonConvert.DeserializeObject<List<employees>>(content);
ListView1.ItemsSource = Items;
}
这是Xamarin.Forms的XAML文件:
<StackLayout BackgroundColor="White">
<ListView x:Name="ListView1" RowHeight="60">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" Padding="8,0,8,0">
<Label Text="{Binding id}" TextColor="#000" FontSize="14" LineBreakMode="TailTruncation" />
<Label Text="{Binding employee_name}" TextColor="#000" LineBreakMode="TailTruncation" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
首先需要创建一个本地模型class :
public class TodoItem
{
public string id { get; set; }
public string employee_name { get; set; }
public string employee_salary{ get; set; }
public bool employee_age { get; set; }
public bool profile_image { get; set; }
}
而在RestService
可以使用TodoItem
:
public List<TodoItem> Items { get; private set; }
public async Task<List<TodoItem>> RefreshDataAsync ()
{
Items = new List<TodoItem> ();
// RestUrl = http://developer.xamarin.com:8081/api/todoitems
var uri = new Uri (string.Format (Constants.RestUrl, string.Empty));
try {
var response = await client.GetAsync (uri);
if (response.IsSuccessStatusCode) {
var content = await response.Content.ReadAsStringAsync ();
Items = JsonConvert.DeserializeObject <List<TodoItem>> (content);
}
} catch (Exception ex) {
Debug.WriteLine (@"ERROR {0}", ex.Message);
}
return Items;
}
最后,您的列表视图 itemsource
可以设置如下:
listView.ItemsSource = await RestService.RefreshDataAsync();
注:这里有一个official sample可以参考
I'm enable to solve this error, i wanted to display listview of API data which is in large amount
在listview中显示大数据,这里有一个分页显示的方法。得到API数据后,保存在本地CacheListData
。不直接设置它到 listView.ItemsSource
。你需要创建一个 ItemListData
从 CacheListData.Which 添加数据 根据你曾经想要 show.When 列表视图滚动到底部,然后显示添加更多滑动方法以重新加载下一页数据。
一般来说,解决方案就是将大量数据缓存到本地first.Then获取数据一点点分页展示Here is a solution link can refer to.
您使用了错误的端点,您应该使用这个端点来检索员工列表
var RestURL = "http://dummy.restapiexample.com/api/v1/employees/";
您可以查看 API documentation