简体   繁体   English

如何在修改列表时迭代 Xamarin.Forms.Maps Map.Pins 列表?

[英]How can I iterate over a Xamarin.Forms.Maps Map.Pins list while modifying the list?

I'm working on a Xamarin.Forms app the utilizes the Maps package.我正在开发一个 Xamarin.Forms 应用程序,它使用地图 package。 The Map object contains an IList Pins which stores the list of Pin objects that contain Label, Position and other properties. Map object 包含一个 IList Pins,其中存储包含 Label、Z52F5E0BC3859BCE5F85881E25 和其他属性的 Pin 对象列表I am attempting to update this Pins list by comparing their Position to a Collection of custom objects that contain the same properties (ID, Position, etc) as well as whether or not they exist on this list anymore, and removing them accordingly.我试图通过将它们的 Position 与包含相同属性(ID、Position 等)的自定义对象的集合进行比较来更新此引脚列表,以及它们是否存在于此列表中,并相应地删除它们。

To elaborate, with every update, I want to iterate through the Pins list, remove any pins that no longer correspond to an object in the collection, add any pins that correspond to new objects in the collection, and change the positions of any pins where the positions of the corresponding object has changed.为了详细说明,每次更新时,我都想遍历 Pins 列表,删除不再与集合中的 object 对应的所有引脚,添加与集合中的新对象对应的所有引脚,并更改任何引脚的位置对应的 object 的位置发生了变化。

I am attempting to do this by iterating over the Pins and comparing accordingly while removing, adding and altering the Pins when necessary.我试图通过迭代引脚并在必要时删除、添加和更改引脚时进行相应的比较来做到这一点。 The problem here is that I get the following error each time a Pin is removed:这里的问题是每次删除 Pin 时都会出现以下错误:

An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code
Collection was modified; enumeration operation may not execute.

This is to be expected when modifying a list that is being iterated over, however all of the solutions available to work around this, such as using Maps.Pins.ToList() when instantiating the foreach loop, using a for loop instead of a foreach loop, or even creating a copy of the Pins list to iterate over while modifying the original, don't solve this problem.在修改正在迭代的列表时这是可以预料的,但是所有可用的解决方案都可以解决这个问题,例如在实例化 foreach 循环时使用 Maps.Pins.ToList() ,使用 for 循环而不是 foreach循环,甚至创建 Pins 列表的副本以在修改原始列表时进行迭代,都不能解决这个问题。

I know some of these solutions work because I've used them to overcome this problem when comparing lists of my custom objects, but for some reason, none of them seem to work with the Map.Pins list.我知道其中一些解决方案有效,因为我在比较自定义对象列表时使用它们来克服这个问题,但由于某种原因,它们似乎都不适用于 Map.Pins 列表。 Can anyone point out what I may be doing wrong, or if there is some detail about Map.Pins list that excludes it from these solutions?谁能指出我可能做错了什么,或者是否有一些关于 Map.Pins 列表的详细信息将其排除在这些解决方案之外? Is there another way to solve this problem?有没有其他方法可以解决这个问题?

For reference here are the ways, in code, I have tried to implement the "remove pins that should no longer exist" functionality:作为参考,这里有一些方法,在代码中,我尝试实现“删除不应该再存在的引脚”功能:

.ToList() .ToList()

foreach (Pin pin in map.Pins.ToList())
            {
                if (!newList.Any(x => x.ID == pin.Label))
                {
                    Debug.WriteLine("Pin " + pin.Label + " is being removed.");
                    map.Pins.Remove(pin);
                }
            }

For Loop循环

            for (int i = 0; i < map.Pins.Count; i++) {
                Debug.WriteLine(map.Pins[i].Label);
                if (!newList.Any(x => x.ID == map.Pins[i].Label))
                {
                    Debug.WriteLine("Pin " + map.Pins[i].Label + " is being removed.");
                    map.Pins.Remove(map.Pins[i]);
                }
            }

Creation of new list创建新列表

List<Pin> oldPins = new List<Pin>();

            foreach (Pin pin in map.Pins)
            {
                oldPins.Add(pin);
            }

foreach (Pin pin in oldPins)
            {
                if (!newList.Any(x => x.ID == pin.Label))
                {
                    Debug.WriteLine("Pin " + pin.Label + " is being removed.");
                    map.Pins.Remove(pin);
                }
            }

// I tried this with the for loop solution as well

Thanks very much in advance首先十分感谢

For the for loop method to work, you need to count backwards, otherwise your index will be thrown off each time you remove an item.要使 for 循环方法起作用,您需要倒数,否则每次删除项目时都会抛出索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM