【问题标题】:Update listview asynchronously异步更新列表视图
【发布时间】:2013-12-03 06:32:16
【问题描述】:

到目前为止,我找不到任何类似的问题,所以我会在这里问:

我有一个绑定到 ListView 的项目源(例如,10 人的列表)。该列表是异步填充的,比方说,列表项将在一秒钟内添加一个项目(人)。你能教我如何让我的 ListView 从第一个项目开始显示项目,而不是等待第十个项目然后将它们一起显示吗?

我们将不胜感激。

提前谢谢你。

XAML:

    <ListView ItemsSource="{Binding tempData, Mode=TwoWay}" Width="1000" Height="1000">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Width="100" Height="50">
                    <TextBlock Text="{Binding name}"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

CS(坦白说我用的是云服务,所以实际代码基本上是这样的:

await app.WorkWith().Data<Person>().GetAll().ExecuteAsync();

所以,我认为上面这行的意思是:

        private async Task<List<Person>> GenerateItem()
    {
        List<Person> tempData = new List<Person>(); 

        tempData.Add(new Person() { name = "name1"});
        await Task.Delay(1000);
        tempData.Add(new Person() { name = "name1" });
        await Task.Delay(1000);
        tempData.Add(new Person() { name = "name1" });
        await Task.Delay(1000);
        tempData.Add(new Person() { name = "name1" });
        await Task.Delay(1000);
        tempData.Add(new Person() { name = "name1" });
        await Task.Delay(1000);
        tempData.Add(new Person() { name = "name1" });
        await Task.Delay(1000);
        tempData.Add(new Person() { name = "name1" });
        await Task.Delay(1000);
        tempData.Add(new Person() { name = "name1" });
        await Task.Delay(1000);
        tempData.Add(new Person() { name = "name1" });
        await Task.Delay(1000);
        return tempData;
    }

最后:

        private async void Populate()
    {

        List<Person> tempData = await GenerateItem();
        listView1.ItemsSource = tempData;            
    }

【问题讨论】:

  • 您想让它们逐渐被列出而无需等待吗?
  • 我认为 ListView 不可能,纯粹是因为它是一个服务器控件,一旦完全绑定数据,服务器才会发送响应。
  • 是的,没错。就在刚才,我偶然发现了 INotifyPropertyChanged。我想这可能与 ListView 上的异步项目更新(显示)有关。
  • 您的具体问题是什么?请发布您到目前为止所做的工作。
  • 如果你有一个非常大的列表要显示,你也可以看到这个codeproject.com/Articles/37641/…

标签: c# xaml asynchronous winrt-async


【解决方案1】:

只需使用 ObservableCollection .... 将项目添加到集合时,会引发 CollectionChanged 并且您的列表视图/网格视图会自行更新

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    相关资源
    最近更新 更多