C# 最大行列表视图xamarin
我有一个列表视图,显示了一个运动队的积分阶梯,我试图只显示前5名球员。这在XAML中是否仍然可以使用 谢谢 瑞安 编辑:这是我的代码 XAML:C# 最大行列表视图xamarin,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我有一个列表视图,显示了一个运动队的积分阶梯,我试图只显示前5名球员。这在XAML中是否仍然可以使用 谢谢 瑞安 编辑:这是我的代码 XAML: 代码隐藏: public partial class PlayerPage : ContentPage { public PlayerPage() { InitializeComponent(); } protected async override
代码隐藏:
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");
}
}
}
public部分类PlayerPage:ContentPage
{
公共游戏网页()
{
初始化组件();
}
受保护的异步重写void OnAppearing()
{
base.OnAppearing();
HttpClient=新的HttpClient();
字符串URL目标=”https://melbourneicewebapi.azurewebsites.net/api/Player_Info/GetPlayer_Info?playerInfo=goals";
字符串URL点=”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)
{
字符串resGoals=“”;
使用(HttpContent contentGoals=responseGoals.Content)
{
Task resultGoals=contentGoals.ReadAsStringAsync();
resGoals=resultGoals.Result;
var GoalsList=Players.playerItems.FromJson(resGoals);
GoalListView.ItemsSource=目标列表;
}
字符串resPoints=“”;
使用(HttpContent contentPoints=responsePoints.Content)
{
Task resultPoints=contentPoints.ReadAsStringAsync();
resPoints=resultPoints.Result;
var PointsList=Players.playerItems.FromJson(响应点);
PointListView.ItemsSource=PointsList;
}
}
其他的
{
等待DisplayAlert(“连接错误”,“请连接到internet并重试”,“确定”);
}
}
}
Players类,其中对象是从JSON字符串创建并添加到列表中的:
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);
}
}
}
公共职业玩家
{
公共部分类playerItems
{
[JsonProperty(“$id”)]
公共长Id{get;set;}
[JsonProperty(“intPlayerID”)]
public int IntPlayerId{get;set;}
[JsonProperty(“strFirstName”)]
公共字符串StrFirstName{get;set;}
[JsonProperty(“strSurname”)]
公共字符串StrSurname{get;set;}
[JsonProperty(“intGamesPlayed”)]
public int IntGamesPlayed{get;set;}
[JsonProperty(“strPosition”)]
公共字符串StrPosition{get;set;}
[JsonProperty(“intPlayerNumber”)]
public int IntPlayerNumber{get;set;}
[JsonProperty(“intGoals”)]
公共int目标{get;set;}
[JsonProperty(“intAssists”)]
公共int IntAssists{get;set;}
[JsonProperty(“intPoints”)]
公共int点{get;set;}
公共字符串StrFullName{
得到
{
返回StrFirstName.Trim()+“”+StrSurname.Trim();
}
}
}
公共部分类playerItems
{
来自json的公共静态列表(字符串json)
{
返回JsonConvert.DeserializeObject(json);
}
}
}
所以我本质上是在访问一个API,它给我两个JSON字符串,包含Players表中的所有数据项,一个按最高点排序,另一个按最高目标排序,然后将其转换为Players类中的对象列表,然后设置到相应的列表视图。在将其分配给ItemsSource之前,使用LINQ选择最高的X分数。我不知道有什么方法可以直接在XAML中过滤数据
var PointsList = Players.PlayersItems.FromJson(resPoints);
var topPoints = PointsList.OrderByDescending(x => x.PointsInt).Take(5).ToList();
PointListView.ItemsSource = topPoints;
纯在XAML中?我不知道,但是你可以很容易地使用LINQ过滤你的ItemsSource,只显示所需的数据。答案当然是你可以。然而,你需要展示你的代码,我们不知道你是如何构建你的应用程序的,你是否在使用MVVM,这一切是否都是在代码中完成的,或者你的结构是什么样子来回答更多的问题抱歉@Saruman,刚刚添加了它。不过,我目前没有使用MVVM模型,它只是非常基本的。我是Xamarin的新手,所以我不喜欢MVVM模型,谢谢你们!它成功了。我可以问一下,当我们在这里的时候,我试图在同一行上显示名称和值,出于某种原因,上面的XAML垂直显示了一个间隙,有没有办法解决这个问题?您使用的是垂直堆栈布局。如果您希望它们并排排列,请使用水平堆叠布局
var PointsList = Players.PlayersItems.FromJson(resPoints);
var topPoints = PointsList.OrderByDescending(x => x.PointsInt).Take(5).ToList();
PointListView.ItemsSource = topPoints;