MudDialog与MudDropZone协同异常:移除操作无法同时生效
问题详情
我实现了一个带拖拽功能的列表,每个列表项都配有编辑按钮,点击按钮会打开MudDialog:
private HashSet<SectionDropZoneItem> visible = new HashSet<SectionDropZoneItem>(); private void OpenDialog(SectionDropZoneItem context) { visible.Add(context); SectionMudDropContainerRef.Refresh(); }
对应的更新方法如下:
async Task Update(SectionDropZoneItem context) { Section newSection = new Section { Name = newName, Description = newDescription }; var oldSection = await repo.Section.GetAsync(context.Id); try { await repo.Section.UpdateAsync(newSection, oldSection); _sectionItems.Add(new SectionDropZoneItem { Zone = "Aktív modulok", Name = newName, Description = newDescription, DepartmentId = context.DepartmentId, DocumentId = context.DocumentId, Position = context.Position }); _sectionItems.Remove(context); visible.Remove(context); } catch (Exception ex) { throw ex; } SectionMudDropContainerRef.Refresh(); }
目前遇到的问题是:_sectionItems.Remove(context)和visible.Remove(context)无法同时执行成功。单独执行其中任意一个都能正常工作,但两者同时执行时就会触发异常。我推测问题根源在于被操作的context对象本身。
补充相关组件代码:
<MudButton Class="" Variant="Variant.Filled" Color="Color.Primary" OnClick="() => OpenDialog(context)" StartIcon="@MudBlazor.Icons.Material.Filled.Edit">编辑</MudButton> <MudDialog IsVisible="visible.Contains(context)" Options="dialogOptions"> <TitleContent> <MudText Typo="Typo.h6"> <MudIcon Icon="@Icons.Material.Filled.Edit" Class="mr-3" /> 编辑@context.Name </MudText> </TitleContent> <DialogContent> <MudTextField T="String" Value="@context.Name" ValueChanged="(string text) => newName = text" Label="名称" Variant="Variant.Text" Required="true"/> <MudTextField T="String" Value="@context.Description" ValueChanged="(string text) => newDescription = text" Label="描述" Variant="Variant.Text" /> </DialogContent> <DialogActions> <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="(() => Close(context))" Class="px-10">关闭</MudButton> <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="(() => Update(context))" Class="px-10">更新</MudButton> </DialogActions> </MudDialog> private class SectionDropZoneItem { public string Zone { get; set; } public string Name { get; init; } public int DepartmentId { get; init; } public int DocumentId { get; init; } public string Description { get; init; } public int Position { get; set; } public int Id { get; set; } }
内容的提问来源于stack exchange,提问作者maruzsam
相关产品推荐
相关产品推荐

