删除所有图钉windows手机

时间:2013-05-28 16:25:55

标签: 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 == MessageBoxResult.OK)
      {
          foreach (UIElement element in map1.Children)
          {
              if (element.GetType() == typeof(Pushpin))
              {
                     map1.Children.Remove(element);
              }  
          }

       }

  }

2 个答案:

答案 0 :(得分:1)

我认为我认为更简单的事情,   仅为图钉创建了一个新图层:

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();
          }

      }

答案 1 :(得分:0)

您必须使用pre-WP8 Map控件,因为WP8版本没有Children属性。我在您的代码中看到的主要问题是您在迭代它时修改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);
        }

    }