Hello everyone,
i am using syncfusion 18.3.0.44 with Blazor WebAssembly (client side, not server side) and .net core 3.1 and i am getting an annoying error with the component SfListBox.
I can provide the following code as example:
"
<SfButton Content="AddElement" OnClick="@AddElementToList" />
<SfListBox @ref="ListBoxChat" TValue="int[]" TItem="Wrapper"
DataSource="@Listed" CssClass="list">
<ListBoxSelectionSettings Mode="SelectionMode.Single"></ListBoxSelectionSettings>
<ListBoxEvents TValue="int[]" TItem="Wrapper"></ListBoxEvents>
<ListBoxFieldSettings Text="Name" Value="ID" />
<ListBoxTemplates TItem="Wrapper">
<ItemTemplate>
<i class="fa fa-@((context.IsSingle) ? "user" : "users") fa-2x"></i>
<span class="top-left">@(context.Name)</span>
</ItemTemplate>
</ListBoxTemplates>
</SfListBox>
@code {
public class Wrapper
{
public int ID { get; set; }
public string Name { get; set; }
public bool IsSingle { get; set; }
public int SortValue => ID;
}
public class MultiObservableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
{
public MultiObservableCollection() : base() { }
public MultiObservableCollection(IEnumerable<T> collection) : base(collection) { }
public MultiObservableCollection(List<T> list) : base(list) { }
public void Reset(IEnumerable<T> range)
{
Items.Clear();
AddRange(range);
}
public void AddRange(IEnumerable<T> range)
{
foreach (var item in range)
{
Items.Add(item);
}
FireChanges();
}
private void FireChanges()
{
OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Count"));
OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
}
}
SfListBox<int[], Wrapper> ListBoxChat;
private bool UpdateListed = true;
private List<Wrapper> Variable = new List<Wrapper>();
private MultiObservableCollection<Wrapper> iListed = new MultiObservableCollection<Wrapper>();
public MultiObservableCollection<Wrapper> Listed
{
get
{
if (UpdateListed)
{
// Working from syncfusion 18.3.0.44 on...
var tmp = new List<Wrapper>();
var fixedList = new List<Wrapper>
{
new Wrapper { ID = -100, Name = "Alpha", IsSingle = true },
new Wrapper { ID = -101, Name = "Beta", IsSingle = true },
new Wrapper { ID = -102, Name = "Gamma", IsSingle = true }
};
tmp.AddRange(fixedList);
if (Variable != null)
tmp.AddRange(Variable);
iListed.Reset(tmp.OrderByDescending(o => o.SortValue));
UpdateListed = false;
}
return iListed;
}
}
void AddElementToList()
{
Variable.Add(new Wrapper { ID = iListed.Count + 1, Name = "Custom", IsSingle = false });
UpdateListed = true;
}
}
"
Every time that i click on the button "AddElement", i got the following error:
"
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Collection was modified; enumeration operation may not execute.
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.Dictionary`2+KeyCollection+Enumerator[TKey,TValue].MoveNext () <0x55823e8 + 0x00016> in <filename unknown>:0
at Syncfusion.Blazor.DropDowns.SfListBox`2[TValue,TItem].OnParametersSetAsync () <0x44c3dc8 + 0x005fa> in <filename unknown>:0
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion (System.Threading.Tasks.Task task) <0x5582b80 + 0x000da> in <filename unknown>:0
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x43caea8 + 0x000b6> in <filename unknown>:0
"
If instead of an SfListBox i use a simple foreach loop
"
<ul>
@foreach (var x in Listed)
{
<li>
@x.Name
</li>
}
</ul>
"
that error does not occurs.
This is just a code example that mimics the behavior I observed in my actual code, where the collection of objects is updated from server input (and not by an interaction from the user).
Am I doing something wrong?