Hi,
Based on my understanding, this kind of problems seems to appear when modifying a collection of items, while iterating over it using aforeach statement. In this case, you are iterating over the Views collection of the region using a foreach and modifying it inside by removing a view from the region.
This is mentioned in the following MSDN article:
A quick approach to avoid this kind of problems is to "find" what elements you want to remove (in this case a view) from the collection and remove them outside theforeach statement. For example:
var views = this.regionManager.Regions[RegionNames.MyRegion].Views;object viewToRemove = null;foreach (var view in views) {var myView = (IMyView)view;if (myView.ViewSortHint == this.ID) {// We found the view we want to remove viewToRemove = myView; } }if(viewToRemove != null) {// We remove the view outside the foreachthis.regionManager.Regions[RegionNames.MyRegion].Remove(viewToRemove); }
Another approach could be to use a for loop instead of a foreach loop.
I hope you find this useful,
Damian Cherubini
http://blogs.southworks.net/dcherubini