-
-
Notifications
You must be signed in to change notification settings - Fork 30
Trying to remove item from BindableListView causes ArgumentOutOfRangeException #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Full trace:
|
Hi. Could you share your |
And why don't you just directly use the public class HomeViewModel : ViewModel
{
[Observable(nameof(Drawings))]
private readonly IReadOnlyProperty<ObservableCollection<DrawingViewModel>> _drawings;
public ObservableCollection<DrawingViewModel> Drawings => _drawings.Value;
public HomeViewModel(IDrawingsProvider drawingsProvider)
{
_drawingsProvider = drawingsProvider;
_drawings = new ReadOnlyProperty<ObservableCollection<DrawingViewModel>>(_drawingsProvider.Drawings);
}
....
} If you don't use the public class HomeViewModel : ViewModel
{
// The field name will be used if you don't provide a property name.
// Names '_drawings' and 'm_drawings' will be auto-converted to 'Drawings'.
[Observable]
private readonly IReadOnlyProperty<ObservableCollection<DrawingViewModel>> _drawings;
public HomeViewModel(IDrawingsProvider drawingsProvider)
{
_drawingsProvider = drawingsProvider;
_drawings = new ReadOnlyProperty<ObservableCollection<DrawingViewModel>>(_drawingsProvider.Drawings);
}
....
} |
Thanks for the fast reply! I'll think of a way of refactoring my code to use ViewModels collection from the start then. My RemoveAll implementation: public static int RemoveAll<T>(
this ObservableCollection<T> coll, Func<T, bool> condition)
{
var itemsToRemove = coll.Where(condition).ToList();
foreach (var itemToRemove in itemsToRemove) coll.Remove(itemToRemove);
return itemsToRemove.Count;
} My DrawvingViewModel: using System;
using ArPaint.Services.Draw;
using UnityMvvmToolkit.Common.Interfaces;
using UnityMvvmToolkit.Core;
using UnityMvvmToolkit.Core.Attributes;
using UnityMvvmToolkit.Core.Interfaces;
namespace ArPaint.UI.ViewModels.Home
{
public class DrawingViewModel : ICollectionItem
{
[Observable(nameof(DrawingName))]
private readonly IProperty<string> _drawingName;
[Observable(nameof(DrawingDescription))]
private readonly IProperty<string> _drawingDescription;
private readonly Action<DrawingData> _selectDrawing;
public int Id { get; }
public DrawingData Drawing { get; }
public ICommand SelectDrawingCommand { get; }
public string DrawingName
{
get => _drawingName.Value;
set => _drawingName.Value = value;
}
public string DrawingDescription
{
get => _drawingDescription.Value;
set => _drawingDescription.Value = value;
}
public DrawingViewModel(DrawingData drawing, Action<DrawingData> selectDrawing)
{
Drawing = drawing;
_selectDrawing = selectDrawing;
_drawingName = new Property<string>(Drawing.Name);
_drawingDescription = new Property<string>(Drawing.Description);
Id = new Guid().GetHashCode();
SelectDrawingCommand = new Command(SelectDrawing);
Drawing.ItemUpdate += Update;
}
private void Update()
{
DrawingName = Drawing.Name;
DrawingDescription = Drawing.Description;
}
private void SelectDrawing()
{
_selectDrawing?.Invoke(Drawing);
}
}
} And my DrawingListView: using ArPaint.UI.ViewModels.Draw;
using ArPaint.UI.ViewModels.Home;
using UnityEngine.UIElements;
using UnityMvvmToolkit.UITK.BindableUIElements;
namespace ArPaint.UI.Views.Home
{
public class DrawingsListView : BindableListView<DrawingViewModel>
{
public new class UxmlFactory : UxmlFactory<DrawingsListView, UxmlTraits>
{
public override string uxmlName => nameof(DrawingsListView);
}
}
} |
Small update: made a test script that uses ViewModels list from the start, I still get the same exception public HomeViewModel(IDrawingsProvider drawingsProvider)
{
_drawingsProvider = drawingsProvider;
_drawings = new ReadOnlyProperty<ObservableCollection<DrawingViewModel>>(BuildDrawingsCollection());
Test();
}
private async void Test()
{
for (var i = 0; i < 5; i++)
{
await Task.Delay(1000);
_drawings.Value.Remove(_drawings.Value.FirstOrDefault());
}
}
private ObservableCollection<DrawingViewModel> BuildDrawingsCollection()
{
var items = new ObservableCollection<DrawingViewModel>();
foreach (var drawing in _drawingsProvider.Drawings)
{
items.Add(new DrawingViewModel(drawing, SelectDrawing));
}
return items;
} P.S I've started getting the same exception after adding new items. (First time adding item to list works fine, second one causes exception) |
Made empty project and made test scripts that add and remove data. On remove throws exception too. Unity package: |
Yes, I found the problem. Unity 2022 calls the Thank you. |
Update: made a fix: |
Oh, haven't seen your reply 😄 |
Fix #11. ArgumentOutOfRange when deleting item from BindableListView in Unity 2022
I've made a BindableListView that has some items that should get added or removed. When trying to remove item from list or calling Clrear on collection, I get ArgumentOutOfRangeException. My ViewModel class:
The text was updated successfully, but these errors were encountered: