My Windows Phone 7 silverlight application, before placing buttons on the map layer, deletes everything that was there before.
I did this in the foreach loop as follows:
try
{
foreach (UIElement p in PushPinLayer.Children)
{
if(p.GetType() == typeof(Pushpin))
{
PushPinLayer.Children.Remove(p);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
This code removes any pushpins as needed, but after the last loop throws an "Invalid Operation" exception, I rewrote it as a for loop:
for (int i = 0; i < PushPinLayer.Children.Count; i++)
{
if (PushPinLayer.Children[i].GetType() == typeof(Pushpin))
{
PushPinLayer.Children.RemoveAt(i);
}
}
Which works fine, however I don't see why foreach throws an error.
source
share