首页 > 解决方案 > 最大行列表视图 xamarin

问题描述

我有一个列表视图,显示运动队的积分阶梯,并试图只显示前 5 名球员。无论如何,这可以在 XAML 中吗?

谢谢,

瑞安

编辑:这是我的代码

XAML:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MIApp.PlayerPage">
    <ContentPage.Content>
        <StackLayout Margin="20,0,0,0" Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
            <Label Text="Player Info" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
            <Grid MinimumHeightRequest="200" RowSpacing="10" Padding="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="50"/>
                    <RowDefinition/>
                    <RowDefinition Height="50"/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                    <Label Text="Goal Leaders" Grid.Row="0" Margin="0"/>
                    <ListView x:Name="GoalListView" Grid.Row="1">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout Orientation="Vertical" Margin="0,0,0,0" Spacing="0" VerticalOptions="Center">
                                            <Label Text="{Binding StrFullName}" HorizontalOptions="Start" VerticalTextAlignment="Center"/>
                                            <Label Text="{Binding IntGoals}" HorizontalOptions="End" VerticalTextAlignment="Center"/>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                    <Label Text="Point Leaders" Grid.Row="2" Margin="0"/>
                    <ListView x:Name="PointListView" HasUnevenRows="true" Grid.Row="3" Margin="0,0,0,0">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout Orientation="Vertical" Margin="0,0,0,0" Spacing="0" VerticalOptions="Center">
                                            <Label Text="{Binding StrFullName}" HorizontalOptions="Start" VerticalTextAlignment="Center"/>
                                            <Label Text="{Binding IntPoints}" HorizontalOptions="End" VerticalTextAlignment="Center"/>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
            </Grid>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

代码背后:

public partial class PlayerPage : ContentPage
    {
        public PlayerPage()
        {
            InitializeComponent();
        }

        protected async override void OnAppearing()
        {
            base.OnAppearing();

            HttpClient client = new HttpClient();
            string urlGoals = "https://melbourneicewebapi.azurewebsites.net/api/Player_Info/GetPlayer_Info?playerInfo=goals";
            string urlPoints = "https://melbourneicewebapi.azurewebsites.net/api/Player_Info/GetPlayer_Info?playerInfo=points";
            var responseGoals = await client.GetAsync(urlGoals);
            var responsePoints = await client.GetAsync(urlPoints);

            if (responsePoints.IsSuccessStatusCode)
            {
                string resGoals = "";
                using (HttpContent contentGoals = responseGoals.Content)
                {
                    Task<string> resultGoals = contentGoals.ReadAsStringAsync();
                    resGoals = resultGoals.Result;
                    var GoalsList = Players.PlayersItems.FromJson(resGoals);
                    GoalListView.ItemsSource = GoalsList;
                }
                string resPoints = "";
                using (HttpContent contentPoints = responsePoints.Content)
                {
                    Task<string> resultPoints = contentPoints.ReadAsStringAsync();
                    resPoints = resultPoints.Result;
                    var PointsList = Players.PlayersItems.FromJson(resPoints);
                    PointListView.ItemsSource = PointsList;
                }
            }
            else
            {
                await DisplayAlert("Connection Error", "Please Connect to the internet and try again", "Ok");
            }
        }
    }

从 JSON 字符串创建对象并添加到列表中的 Players 类:

public class Players
    {
        public partial class PlayersItems
        {
            [JsonProperty("$id")]
            public long Id { get; set; }

            [JsonProperty("intPlayerID")]
            public int IntPlayerId { get; set; }

            [JsonProperty("strFirstName")]
            public string StrFirstName { get; set; }

            [JsonProperty("strSurname")]
            public string StrSurname { get; set; }

            [JsonProperty("intGamesPlayed")]
            public int IntGamesPlayed { get; set; }

            [JsonProperty("strPosition")]
            public string StrPosition { get; set; }

            [JsonProperty("intPlayerNumber")]
            public int IntPlayerNumber { get; set; }

            [JsonProperty("intGoals")]
            public int IntGoals { get; set; }

            [JsonProperty("intAssists")]
            public int IntAssists { get; set; }

            [JsonProperty("intPoints")]
            public int IntPoints { get; set; }

            public string StrFullName {
                get
                {
                    return StrFirstName.Trim() + " " + StrSurname.Trim();
                }
            }

        }

        public partial class PlayersItems
        {
            public static List<PlayersItems> FromJson(string json)
            {
                return JsonConvert.DeserializeObject<List<PlayersItems>>(json);
            }
        }
    }

所以我实际上在做的是访问一个 API,它为我提供了 Players 表中所有数据条目的两个 JSON 字符串,一个按最高点排序,另一个按最高目标排序,然后将其转换为来自的对象列表Players Class,然后设置为适当的列表视图。

标签: c#xamlxamarinxamarin.forms

解决方案


在将它们分配给 ItemsSource 之前,使用 LINQ 选择前 X 分数。我不知道有什么方法可以直接在 XAML 中过滤数据

var PointsList = Players.PlayersItems.FromJson(resPoints);

var topPoints = PointsList.OrderByDescending(x => x.PointsInt).Take(5).ToList();

PointListView.ItemsSource = topPoints;

推荐阅读