1

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

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

       }

  }
4

2 回答 2

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

      }
于 2013-06-05T09:35:40.200 回答
0

您必须使用 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);
        }

    }
于 2013-05-29T06:43:07.473 回答