2013-05-28 38 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的代碼:) –

相關問題