c# - UWP 中的 ListView 在 INotifyCollectionChanged 报告批量更改时出现意外行为

标签 c# xaml listview uwp

我正在尝试创建一个实现 INotifyCollectionChanged 的自定义集合并随时间报告内部项目列表的批量更改。这是一个更大项目的一部分,但我已经设法创建了一个示例应用程序来演示这个问题。请按照以下步骤操作,解释在最后:

1) 打开 visual studio 2015 并使用 Build 14393 创建一个空白的通用 Windows 应用并将其命名为“SampleApp”,这样我们就有了相同的命名空间。

2) 复制以下代码到Main.xaml

<Page x:Class="SampleApp.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="using:SampleApp"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{x:Bind Items}" Margin="40"/>
    </Grid>
</Page>

3) 复制以下代码到Main.xaml.cs :

using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading.Tasks;
using Windows.UI.Xaml;

namespace SampleApp
{
    public sealed partial class MainPage
    {
        public FakeCollection Items { get; } = new FakeCollection();

        public MainPage()
        {
            InitializeComponent();

            Loaded += OnLoaded;
        }

        private async void OnLoaded(object sender, RoutedEventArgs e)
        {
            await Items.Begin();
        }
    }

    public class FakeCollection : List<string>, INotifyCollectionChanged, INotifyPropertyChanged
    {
        public event NotifyCollectionChangedEventHandler CollectionChanged;
        public event PropertyChangedEventHandler PropertyChanged;


        public async Task Begin()
        {
            var set1 = CreateSet(1, 10);
            var set2 = CreateSet(11, 15);

            AddRange(set1);

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Count"));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item[]"));
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, set1, 0));


            await Task.Delay(2000);

            InsertRange(5, set2);

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Count"));
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item[]"));
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, set2, 5));
        }

        public List<string> CreateSet(int start, int end)
        {
            var list = new List<string>();

            for (var i = start; i <= end; i++)
            {
                list.Add($"Item {i}");
            }

            return list;
        }
    }
}

FakeCollection只是一个List<string>并实现 INotifyPropertyChangedINotifyCollectionChanged .它用于 ListView在 xaml 中绑定(bind)。

当主页加载时,Begin()方法被调用,它只是创建一个名为“Item 1”、“Item 2”、...“Item 10”的字符串列表,将它们添加到列表中,然后通知所有 10 个项目的集合更改。这很好用。

但是,等待 2 秒后,创建了另一个集合“Item 11”、“Item 12”、...“Item 15”,并将它们插入到索引 5 处。我们再次在位置 5 报告集合更改新的字符串集。

我希望 ListView显示在 FakeCollection 的内部集合中找到的确切顺序,即:

项目 1,2,3,4,5,11,12,13,14,15,6,7,8,9,10

相反,它显示为

项目 1,2,3,4,5,11,6,7,8,9,10,7,8,9,10 ????

这里发生了什么?看起来它添加了 raised 事件中的第一个项目,然后只是重复了最后四个!

制作ListView有技巧吗?使用批量集合更改?

最佳答案

有趣!当您尝试一次添加多个项目时,看起来 CollectionChanged 不起作用。我想你将不得不做这样的事情-

for (var i = 0; i < set2.Count; i++)
{
    CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, set2, i + 5));
}

关于c# - UWP 中的 ListView 在 INotifyCollectionChanged 报告批量更改时出现意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42272883/

相关文章:

c# - 在缓存中找不到类型

c# - 发送包含相同附件的多封电子邮件

c# - Xamarin.Forms:IValueConverter 只工作一次

android - 如何像 ios 的 UITableview 一样在 android listview 中绘制节标题?

c# - 从 Access 数据库中检索图像

c# - 从sql server调用方法名和方法体

XAML 2009 和 .NET 4.6/Windows 10

xaml - 我的 Xamarin Forms App.Shell 中的颜色配置缺少什么?

android - ListView 项目状态未在 Android 中保存在滚动中

flutter - 集成测试中的ListView(Flutter)