Xamarin 中的 ListView 使用 RestApi [英] ListView in Xamarin using RestApi

查看:28
本文介绍了Xamarin 中的 ListView 使用 RestApi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以解决这个错误.我想显示大量 API 数据的 ListView.

I'm enable to solve this error. I wanted to display ListView of API data which is in large amount.

例如 API 包含这种类型的数据:

[{"id":"666","employee_name":"xyz","employee_salary":"123","employee_age":"23","profile_image":""}]

错误截图:

我在将 JSON 转换为 c# 后制作的 Class.cs

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

    }

这是 LoadData() 用于调用 API 的 XAML.cs 文件

This is the XAML.cs file where LoadData() is using for calling 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>

推荐答案

首先需要创建一个本地模型类:

First need to create a local model 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 可以设置如下:

And last where your listview itemsource can set as follow:

listView.ItemsSource = await RestService.RefreshDataAsync();

注意:这是您可以参考的官方示例

Note: Here is a official sample you can refer to.

我可以解决这个错误,我想显示大量 API 数据的列表视图

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中添加数据.根据你添加的数据的数量想要显示.当listview滚动到底部时,然后显示添加更多滑动方法以重新加载下一页数据.

Showing large data in listview,here is a paging show method.After getting API data, saving them in local CacheListData.Not directly set it to listView.ItemsSource .And you need create a ItemListData to add data from CacheListData.Which the count of added data according to you once want to show.When listview scroll to bottom ,then show Add More Swipe method to reload next page data.

一般来说,解决方案是先将lagre数据缓存到本地,然后逐页获取数据显示.这里有一个解决方案链接,可以参考

Generally, Solution is to cache lagre data to local first.Then get data a little by page to show.Here is a solution link can refer to.

这篇关于Xamarin 中的 ListView 使用 RestApi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆