gpt4 book ai didi

c# - 如何从 UWP 应用程序的列表中删除项目?

转载 作者:行者123 更新时间:2023-12-03 10:41:21 24 4
gpt4 key购买 nike

我对 C# 和 XAML 都很陌生,但我需要为我的学校项目创建一个 UWP 应用程序。我所拥有的基本上是一个简单的家具数据库。我可以毫无问题地将项目添加到我的列表中,我也可以列出这些项目,或在列表中搜索,但是当我尝试删除一个时,我总是会收到此错误:Collection was modified; enumeration operation may not execute.我正在使用 MVVM 技术,我认为这是问题所在,我并不完全理解它。这是我所拥有的:

public class ItemManager
{
public readonly CategoryList Categories = new CategoryList();
public readonly FurnitureList Furnitures = new FurnitureList();
这只是一个基本的项目管理器,它有 Furnitures列表, FurnitureListObservableCollection .
我有 2 个 ViewModel,它们是:
public class ListItemViewModel : ObservableObjectBase
{

public Furniture Model { get; private set; }

public ListItemViewModel(Furniture model)
{
Model = model;
model.PropertyChanged += Model_PropertyChanged;
}

public ListItemViewModel()
{

}

private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(e.PropertyName == nameof(Model.FurnitureID) || e.PropertyName == nameof(Model.Description) || e.PropertyName == nameof(Model.Quantity))
{
Notify(nameof(FurnitureToShow));
Notify(nameof(FurnitureIDAndCategory));
Notify(nameof(FurnitureDescription));
Notify(nameof(FurnitureQuantityToDisplay));
}
if(e.PropertyName == nameof(Model.Category))
{
Notify(nameof(IsSensitive));
}
}
这里 FurnitureToShow而其他只是字符串,我相信它们在这里不相关。
这是另一个 ViewModel(我在这个中遇到了错误)
 public class ListViewModel : ObservableCollection<ListItemViewModel>
{
public ListViewModel(FurnitureList model)
{
model.CollectionChanged += Model_CollectionChanged;
}

private void Model_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
foreach (Furniture f in e.NewItems)
this.Add(new ListItemViewModel(f));
if(e.OldItems != null)
{
var toRemove = this.Items.Where(i => e.OldItems.Contains(i.Model));
foreach (var vm in toRemove) **This is the line where the error pops up**
this.Remove(vm);
}
}
}
我想要做的是,当我从 ListView 中选择一个项目时,我按下删除按钮,所选项目将从原始集合中删除。这是 XAML 代码:
<StackPanel Orientation="Horizontal" Margin="4" Height="500" VerticalAlignment="Top">
<ListView SelectionChanged="ItemGotSelected_Filter"
SelectedItem="{x:Bind selectedItem, Mode=TwoWay}"
SelectedIndex="{x:Bind selectedIndex, Mode=TwoWay}"
x:Name="FilteredListView"
ItemTemplate="{StaticResource ContactListViewTemplate}"
SelectionMode="Single"
ShowsScrollingPlaceholders="True"
Height="500"
Width="230"
BorderThickness="1"
BorderBrush="{ThemeResource SystemControlForegroundBaseMediumLowBrush}"/>

<StackPanel Width="170">
<TextBlock Margin="8,8,8,4" Style="{ThemeResource BaseTextBlockStyle}">Filter by...</TextBlock>
<TextBox x:Name="FilterByID" Width="150" Header="Furniture ID" Margin="8"
HorizontalAlignment="Left" TextChanged="OnFilterChanged"/>
<TextBox x:Name="FilterByCategory" Width="150" Header="Category" Margin="8"
HorizontalAlignment="Left" TextChanged="OnFilterChanged"/>
<Button Margin="8,20,8,4" x:Name="deleteButton" Width="80" Height="30" HorizontalAlignment="Center" Style="{StaticResource ButtonRevealStyle}" Content="Delete item" FontSize="14" Padding="1" IsEnabled="False" Click="deleteButton_Click">
</Button>
</StackPanel>
我很确定绑定(bind)的 SelectedItem 代表我要删除的实际家具项目,但如果我错了,请纠正我。所以最后这里是后面的 C# 代码:
public sealed partial class ThreeLineList : UserControl
{
public FurnitureList allFurnitures;

public ListViewModel AllFurnituresList { get; private set; }

public Furniture selectedItem = null;
public int selectedIndex;

private ObservableCollection<Furniture> filteredFurnitures = new ObservableCollection<Furniture>();

public ThreeLineList()
{
this.InitializeComponent();
}

public void SetModel(ItemManager itemManager)
{
this.allFurnitures = itemManager.Furnitures;
this.AllFurnituresList = new ListViewModel(allFurnitures);
this.filteredFurnitures = new ObservableCollection<Furniture>(allFurnitures);

FilteredListView.ItemsSource = filteredFurnitures;
}

private void ItemGotSelected_Filter(object sender, SelectionChangedEventArgs e)
{
deleteButton.IsEnabled = true;
}

private void deleteButton_Click(object sender, RoutedEventArgs e)
{
allFurnitures.Remove(selectedItem);
//filteredFurnitures.Remove(selectedItem);
}

filteredFurnitures并且与过滤器有关的所有内容都不相关,该部分运行良好,因此我没有包含它。所以在我的脑海中,我的 ListView 中的项目实际上在原始列表中,因为 allFurnitures列表已连接到项目管理器,如果我使用另一个 View 将项目添加到列表中,它们会正确显示。这就是我不明白为什么当我尝试从该列表中删除所选项目时收到错误消息的原因。

最佳答案

枚举(例如在 foreach 循环中)是顺序的。删除元素时,会破坏可能导致“索引超出范围”异常的序列。请改用 for 循环。

关于c# - 如何从 UWP 应用程序的列表中删除项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64932906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com