2013-05-28 39 views
1

我试着删除所有从地图图钉和它没有将其删除(什么也没发生),任何帮助,将不胜感激删除所有图钉的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); 
       } 
      } 

     } 

    } 
+1

你试过'map1.Children.Clear()'?另外,你是如何将图钉添加到地图的? –

回答

0

您必须使用预WP8地图控制,因为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); 
     } 

    } 
1

我想通了spomething简单,我认为, 只为图钉作出了新的层:

MapLayer pushpin_layer = new MapLayer(); 

添加图钉到该图层:

pushpin_layer.Children.Add(random_point); 

add remove the children(pu shpins):

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

     } 
+0

不简单,只是在上下文的差异。稍后,您可能会在此图层中添加更多元素('Children'),然后您想要选择要删除的元素。所以你最终会得到类似于@ Oren's的代码:) –

相关问题