C# 卸下windows phone上的所有图钉

C# 卸下windows phone上的所有图钉,c#,windows-phone,C#,Windows Phone,我试图从地图上移除所有图钉,但没有移除它们(什么都没有发生),任何帮助都将不胜感激 private void Remove_all_PushPins_click(object sender, EventArgs e) { MessageBoxResult m = MessageBox.Show("All PushPins will be deleted", "Alert", MessageBoxButton.OKCancel); if (m == MessageBo

我试图从地图上移除所有图钉,但没有移除它们(什么都没有发生),任何帮助都将不胜感激

 private void Remove_all_PushPins_click(object sender, EventArgs e)
 {
      MessageBoxResult m =  MessageBox.Show("All PushPins will be deleted", "Alert", MessageBoxButton.OKCancel);
      if (m == MessageBoxResult.OK)
      {
          foreach (UIElement element in map1.Children)
          {
              if (element.GetType() == typeof(Pushpin))
              {
                     map1.Children.Remove(element);
              }  
          }

       }

  }

您必须使用WP8之前的映射控件,因为WP8版本没有
Children
属性。我在代码中看到的主要问题是,在迭代过程中修改
子集合
,这将引发
InvalidOperationException

我根据您的示例模拟了一些代码,这些代码应该可以工作:

    private void myMap_Tap(object sender, GestureEventArgs e)
    {
        // removal queue for existing pins
        var toRemove = new List<UIElement>();

        // iterate through all children that are PushPins. Could also use a Linq selector
        foreach (var child in myMap.Children)
        {
            if (child is Pushpin)
            {
                // queue this child for removal
                toRemove.Add(child);
            }
        }

        // now do the actual removal
        foreach (var child in toRemove)
        {
            myMap.Children.Remove(child);
        }

        // now add in 10 new PushPins
        var rand = new Random();

        for (int i = 0; i < 10; i++)
        {
            var pin = new Pushpin();

            pin.Location = new System.Device.Location.GeoCoordinate() { Latitude = rand.Next(90), Longitude = rand.Next(-180, 180) };

            myMap.Children.Add(pin);
        }

    }
private void myMap_轻触(对象发送者、GestureEventArgs e)
{
//现有PIN的删除队列
var toRemove=新列表();
//遍历作为图钉的所有子级。也可以使用Linq选择器
foreach(myMap.Children中的变量child)
{
如果(孩子是图钉)
{
//将此子项排队以删除
删除。添加(子项);
}
}
//现在进行实际移除
foreach(toRemove中的变量子项)
{
myMap.Children.Remove(child);
}
//现在添加10个新的图钉
var rand=new Random();
对于(int i=0;i<10;i++)
{
var销=新的图钉();
pin.Location=new System.Device.Location.GeoCoordinate(){纬度=rand.Next(90),经度=rand.Next(-180180)};
myMap.Children.Add(pin);
}
}

您必须使用WP8之前的映射控件,因为WP8版本没有
子属性。我在代码中看到的主要问题是,在迭代过程中修改
子集合
,这将引发
InvalidOperationException

我根据您的示例模拟了一些代码,这些代码应该可以工作:

    private void myMap_Tap(object sender, GestureEventArgs e)
    {
        // removal queue for existing pins
        var toRemove = new List<UIElement>();

        // iterate through all children that are PushPins. Could also use a Linq selector
        foreach (var child in myMap.Children)
        {
            if (child is Pushpin)
            {
                // queue this child for removal
                toRemove.Add(child);
            }
        }

        // now do the actual removal
        foreach (var child in toRemove)
        {
            myMap.Children.Remove(child);
        }

        // now add in 10 new PushPins
        var rand = new Random();

        for (int i = 0; i < 10; i++)
        {
            var pin = new Pushpin();

            pin.Location = new System.Device.Location.GeoCoordinate() { Latitude = rand.Next(90), Longitude = rand.Next(-180, 180) };

            myMap.Children.Add(pin);
        }

    }
private void myMap_轻触(对象发送者、GestureEventArgs e)
{
//现有PIN的删除队列
var toRemove=新列表();
//遍历作为图钉的所有子级。也可以使用Linq选择器
foreach(myMap.Children中的变量child)
{
如果(孩子是图钉)
{
//将此子项排队以删除
删除。添加(子项);
}
}
//现在进行实际移除
foreach(toRemove中的变量子项)
{
myMap.Children.Remove(child);
}
//现在添加10个新的图钉
var rand=new Random();
对于(int i=0;i<10;i++)
{
var销=新的图钉();
pin.Location=new System.Device.Location.GeoCoordinate(){纬度=rand.Next(90),经度=rand.Next(-180180)};
myMap.Children.Add(pin);
}
}

我觉得这更简单, 仅为图钉创建新层:

MapLayer pushpin_layer = new MapLayer();
将图钉添加到该层:

pushpin_layer.Children.Add(random_point);
 private void Remove_all_PushPins_click(object sender, EventArgs e)
     {
          MessageBoxResult m =  MessageBox.Show("All PushPins will be deleted", "Alert", MessageBoxButton.OKCancel);
          if (m == MessageBoxResult.OK)
          {
                  pushpin_layer.Children.Clear();
          }

      }
添加或删除该层的子项(图钉):

pushpin_layer.Children.Add(random_point);
 private void Remove_all_PushPins_click(object sender, EventArgs e)
     {
          MessageBoxResult m =  MessageBox.Show("All PushPins will be deleted", "Alert", MessageBoxButton.OKCancel);
          if (m == MessageBoxResult.OK)
          {
                  pushpin_layer.Children.Clear();
          }

      }

我觉得这更简单, 仅为图钉创建新层:

MapLayer pushpin_layer = new MapLayer();
将图钉添加到该层:

pushpin_layer.Children.Add(random_point);
 private void Remove_all_PushPins_click(object sender, EventArgs e)
     {
          MessageBoxResult m =  MessageBox.Show("All PushPins will be deleted", "Alert", MessageBoxButton.OKCancel);
          if (m == MessageBoxResult.OK)
          {
                  pushpin_layer.Children.Clear();
          }

      }
添加或删除该层的子项(图钉):

pushpin_layer.Children.Add(random_point);
 private void Remove_all_PushPins_click(object sender, EventArgs e)
     {
          MessageBoxResult m =  MessageBox.Show("All PushPins will be deleted", "Alert", MessageBoxButton.OKCancel);
          if (m == MessageBoxResult.OK)
          {
                  pushpin_layer.Children.Clear();
          }

      }

您是否尝试过
map1.Children.Clear()
?另外,您是如何将图钉添加到地图的?您是否尝试过
map1.Children.Clear()
?另外,你是如何将图钉添加到地图的?不简单,只是上下文不同而已。稍后,您可能会在该层中包含更多元素(“子元素”),然后您需要选择要删除的元素。因此,您将得到类似于上面@Oren的代码:)不是更简单,只是上下文中的差异。稍后,您可能会在该层中包含更多元素(“子元素”),然后您需要选择要删除的元素。因此,您将得到类似于上面@Oren的代码:)