【问题标题】:Pagination in ListView (UWP)ListView 中的分页 (UWP)
【发布时间】:2017-11-22 13:13:47
【问题描述】:

我有 UWP,而我在 xaml 上有 ListView。

这是我如何接收 json 的代码,将其设置为 List

 public class TopPostsViewModel : INotifyPropertyChanged
{
    private List<Child> postsList;

    public List<Child> PostsList
    {
        get { return postsList; }
        set { postsList = value; OnPropertyChanged(); }
    }

    public TopPostsViewModel()
    {
        Posts_download();
    }


    public async void Posts_download()
    {
        string url = "https://www.reddit.com/top/.json?count=50";

        var json = await FetchAsync(url);


        RootObject rootObjectData = JsonConvert.DeserializeObject<RootObject>(json);
        PostsList = new List<Child>(rootObjectData.data.children);


    }
    private async Task<string> FetchAsync(string url)
    {
        string jsonString;

        using (var httpClient = new System.Net.Http.HttpClient())
        {
            var stream = await httpClient.GetStreamAsync(url);
            StreamReader reader = new StreamReader(stream);
            jsonString = reader.ReadToEnd();
        }

        return jsonString;
    }
    public event PropertyChangedEventHandler PropertyChanged;

    //[NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

}

在 XAML 中我这样显示:

 <ListView x:Name="OrderList"  ItemsSource="{Binding PostsList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="GridInf" Height="204" BorderBrush="#FFFBF8F8" BorderThickness="0,0,0,1">
                    <Image x:Name="Image" HorizontalAlignment="Left" Height="200"  Width="200" Tapped="Image_Tapped">
                        <Image.Source>
                            <BitmapImage UriSource="{Binding data.thumbnail}" />
                        </Image.Source>
                    </Image>
                    <TextBlock Text="{Binding data.title}" HorizontalAlignment="Left" Margin="252,10,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Height="134" Width="1028"/>
                    <TextBlock HorizontalAlignment="Left" Margin="252,139,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Height="97" Width="218">
                        <Run Text="Comments:  "/>
                        <Run Text="{Binding data.num_comments}"/>
                    </TextBlock>
                    <TextBlock HorizontalAlignment="Left" Margin="470,134,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="102" Width="312"/>
                    <TextBlock HorizontalAlignment="Left" Margin="787,139,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Height="97" Width="493">
                        <Run Text="Author:   "/>
                        <Run Text="{Binding data.author}"/>
                    </TextBlock>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>



</Grid>

我需要对这个 ListView 进行分页 - 每页仅显示 10 个帖子(现在我的列表中有 50 个帖子)

我该怎么做?

【问题讨论】:

  • 好吧。我检查它并通过 NuGet 安装。当我使用这个&lt;telerikPrimitives:RadPaginationControl PageProvider="{Binding ElementName=OrderList}" Height="30" Margin="450"/&gt; 时,它只是在列表框中切换项目。我需要每页显示 10 个项目。并从前十名切换到第二名。我怎么能意识到这一点?@JustinXL
  • @Logan 你想什么时候显示接下来的 10 个项目?
  • 看,现在我显示列表中的所有项目(50 个项目)我需要默认显示 10 个项目,但有类似按钮,它将用新的 10 个项目等更新我的 xaml。@VijayNirmal
  • @Logan 使用两个列表(一个用于所有项目,另一个用于要显示的 10 个项目)。使用(from p in _people select p).Skip(pageIndex * pageSize).Take(pageSize)这行代码得到接下来要显示的10个项目
  • 我该如何实现这个?@VijayNirmal

标签: c# xaml listview pagination uwp


【解决方案1】:

使用两个列表(一个用于所有项目,另一个用于要显示的 10 个项目)

private List<ItemSource> postsList = new List<ItemSource>(); //Given List
private List<ItemSource> displayPostsList = new List<ItemSource>(); //List to be displayed in ListView
int pageIndex = -1;
int pageSize = 10; //Set the size of the page

private void NextButton_Click(object sender, RoutedEventArgs e)
{
    pageIndex++;
    displayPostsList = postsList.Skip(pageIndex * pageSize).Take(pageSize).ToList();
}

private void PreviousButton_Click(object sender, RoutedEventArgs e)
{
    pageIndex--;
    displayPostsList = postsList.Skip(pageIndex * pageSize).Take(pageSize).ToList();
}

//Call NextButton_Click in page Constructor to show defalut 10 items
NextButton_Click(null, null);

不要忘记使用INotifyPropertyChanged 来更新ListViewItems(或)使用ObservableCollection 代替List 并使用this 答案将List 转换为ObservableCollection

【讨论】:

  • 非常感谢,它有帮助。我还有一个问题。你能帮我解决这个问题吗? https://stackoverflow.com/questions/44643498/convert-unix-timestamp-to-normal-date-uwp
  • @Logan 如果这个答案有助于考虑标记为答案
  • 这是一个 hack,完全违背了分页的目的。
猜你喜欢
  • 1970-01-01
  • 2020-01-16
  • 2018-01-07
  • 2014-03-25
  • 1970-01-01
  • 2017-01-24
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多