我想在我的ListView中每页设置4个项目,我总共有5个项目,我的代码工作正常,但是当我执行代码时,我总是得到5个项目而不是4个项目:
ObservableCollection<NvBarberry.Models.One_searchs> customers = new ObservableCollection<NvBarberry.Models.One_searchs>();
ObservableCollection<NvBarberry.Models.One_searchs> filtered = new ObservableCollection<NvBarberry.Models.One_searchs>();
int currentPageIndex = 0;
int itemPerPage = 4;
int totalPage = 0;
private void Page_Loaded(object sender, RoutedEventArgs e)
{
getLocalsList();
}
private void ShowCurrentPageIndex()
{
this.tbCurrentPage.Text = (currentPageIndex + 1).ToString();
if (Convert.ToInt32(tbCurrentPage.Text.ToString()) == totalPage)
{
this.tbNextPage.Text = "";
}
else
{
this.tbNextPage.Text = (currentPageIndex + 2).ToString();
}
NextBorder.Opacity = 0.5;
}
void Filter()
{
ObservableCollection<NvBarberry.Models.One_searchs> filtered = new ObservableCollection<NvBarberry.Models.One_searchs>();
foreach (NvBarberry.Models.One_searchs item in customers)
{
int index = customers.IndexOf(item);
if (index >= itemPerPage * currentPageIndex && index < itemPerPage * (currentPageIndex + 1))
{
filtered.Add(item);
}
}
this.listme.DataContext = filtered;
}
private void btnFirst_Click(object sender, RoutedEventArgs e)
{
// Display the first page
if (currentPageIndex != 0)
{
currentPageIndex = 0;
Filter();
}
ShowCurrentPageIndex();
}
private void btnPrev_Click(object sender, RoutedEventArgs e)
{
// Display previous page
if (currentPageIndex > 0)
{
currentPageIndex--;
Filter();
}
ShowCurrentPageIndex();
}
private void btnNext_Click(object sender, RoutedEventArgs e)
{
// Display next page
if (currentPageIndex < totalPage - 1)
{
currentPageIndex++;
Filter();
}
ShowCurrentPageIndex();
}
private void btnLast_Click(object sender, RoutedEventArgs e)
{
// Display the last page
if (currentPageIndex != totalPage - 1)
{
currentPageIndex = totalPage - 1;
Filter();
}
ShowCurrentPageIndex();
}
private async void getLocalsList()
{
try
{
string UriString2 = "MyWebService";
var http = new HttpClient();
http.MaxResponseContentBufferSize = Int32.MaxValue;
var response = await http.GetStringAsync(UriString2);
var rootObject1 = JsonConvert.DeserializeObject<Nv.Models.RootObject>(response);
listme.ItemsSource = rootObject1.one_searchs;
int itemcount = 5;
// Calculate the total pages
totalPage = itemcount / itemPerPage;
if (itemcount % itemPerPage != 0)
{
totalPage += 1;
}
Filter();
ShowCurrentPageIndex();
}
catch (Exception e)
{
e.Message.ToString();
}
这是我的xaml代码:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<ListView x:Name="listme" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ID}"></TextBlock>
<TextBlock Text="{Binding Name}"></TextBlock>
<TextBlock Text="{Binding Age}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
<Grid Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal">
<Button Height="50" HorizontalAlignment="Left" Name="btnFirst"
VerticalAlignment="Bottom" Width="75" Click="btnFirst_Click" Background="Transparent">
<Button.Content>
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="25"/>
</Button.Content>
</Button>
<Button Height="50" HorizontalAlignment="Left" Name="btnPrev" Background="Transparent"
VerticalAlignment="Bottom" Click="btnPrev_Click">
<Button.Content>
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="25"/>
</Button.Content>
</Button>
<Border Name="CurrentBorder" Height="50" Width="50" Background="Purple">
<TextBlock Name="tbCurrentPage" Height="50" Width="50"></TextBlock>
</Border>
<Border Name="NextBorder" Height="50" Width="50" Background="Purple">
<TextBlock Name="tbNextPage" Height="50" Width="50"></TextBlock>
</Border>
<Button Height="50" Name="btnNext" Background="Transparent"
VerticalAlignment="Bottom" Click="btnNext_Click">
<Button.Content>
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="25"/>
</Button.Content>
</Button>
<Button Height="50" HorizontalAlignment="Right" Name="btnLast" Background="Transparent"
VerticalAlignment="Bottom" Click="btnLast_Click">
<Button.Content>
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="25"/>
</Button.Content>
</Button>
</StackPanel>
</Grid>
</Grid>
我遇到的问题是每页只设置4个项目,而不是将它们全部放在我的ListView中的一个页面中,我用我的代码获取,我已将“itemPerPage”变量设置为4但是我总是遇到同样的问题 所以,请问我如何更正我的代码,在ListView中每页只放4个项目 谢谢你的帮助
答案 0 :(得分:1)
索引是0基本值而不是1,并且您在if条件中使用&gt; = 。只需删除=,您将在第一页上获得4个项目,在第二页上获得8个项目。
int index = customers.IndexOf(item);
if (index > itemPerPage * currentPageIndex && index < itemPerPage * (currentPageIndex + 1))
{
filtered.Add(item);
}
else { break; }//all items are added no need to traverse all the list.
<强>更新强>
int index = 0;
foreach (NvBarberry.Models.One_searchs item in customers)
{
if (index > itemPerPage * currentPageIndex && index < itemPerPage * (currentPageIndex + 1))
{
filtered.Add(item);
} else {break; }
index++;
}
更新2
private async void getLocalsList()
{
try
{
string UriString2 = "MyWebService";
var http = new HttpClient();
http.MaxResponseContentBufferSize = Int32.MaxValue;
var response = await http.GetStringAsync(UriString2);
var rootObject1 = JsonConvert.DeserializeObject<Nv.Models.RootObject>(response);
///No need, as data will be assigned filter mehtod listme.ItemsSource = rootObject1.one_searchs;
int itemcount = 5;
// Calculate the total pages
totalPage = itemcount / itemPerPage;
if (itemcount % itemPerPage != 0)
{
totalPage += 1;
}
currentPageIndex = 1
Filter();
ShowCurrentPageIndex();
}
catch (Exception e)
{
e.Message.ToString();
}