ObservableCollectionEx icon indicating copy to clipboard operation
ObservableCollectionEx copied to clipboard

System.Reflection.TargetInvocationException:“Exception has been thrown by the target of an invocation.”

Open CodingOctocat opened this issue 2 years ago • 3 comments

I have a ObservableCollectionEx: [1, 2, 3], if i remove 2 and 3 in DelayNotifications, then throw

System.Reflection.TargetInvocationException:“Exception has been thrown by the target of an invocation.” InnerEx: NotSupportedException(Range operation is not supported)

<Window x:Class="ObservableCollectionExIssue5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ObservableCollectionExIssue5"
        Title="MainWindow"
        Width="300"
        Height="450"
        d:DataContext="{d:DesignInstance Type=local:MainWindow,
                                         IsDesignTimeCreatable=True}"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        FontSize="24"
        mc:Ignorable="d">
    <Grid>
        <ListBox x:Name="lst"
                 ItemsSource="{Binding Samples}"
                 SelectionMode="Extended" />

        <Button Height="50"
                VerticalAlignment="Bottom"
                Click="Button_Click"
                Content="Delete selected items" />
    </Grid>
</Window>
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace ObservableCollectionExIssue5;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public ObservableCollectionEx<int> Samples { get; set; } = new() { 1, 2, 3, 4, 5 };

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var toBeDeleted = lst.SelectedItems.Cast<int>().ToImmutableList();

        using var d = Samples.DelayNotifications();

        for (int i = 0; i < toBeDeleted.Count; i++)
        {
            int item = toBeDeleted[i];
            d.Remove(item);
        }
    }
}

CodingOctocat avatar Jul 20 '23 09:07 CodingOctocat

Can you create a test case with this behavior?

ENikS avatar Jul 20 '23 15:07 ENikS

I can remove one item at a time, however, if I select two or more to remove, an exception will be thrown.

动画1

Demo: ObservableCollectionExIssue5.zip

CodingOctocat avatar Jul 21 '23 11:07 CodingOctocat

It seems there is a problem with the package. I've tried it with just a source, and it worked fine. I'll look into it further, but for now, you could just copy the cs file from this repo instead of referencing the package.

ENikS avatar Jul 22 '23 17:07 ENikS