C# 如何从Bing地图上动态创建的MapLayers中清除图钉?

C# 如何从Bing地图上动态创建的MapLayers中清除图钉?,c#,dynamic,bing-maps,C#,Dynamic,Bing Maps,我有以下代码: DataLayer.Children.Clear(); …从设计时映射层中删除图钉,声明如下: <bm:Map Credentials="CuellarDobsonMcNallyPalmer420s" x:Name="photraxMap" Margin="0,0,0,0" > <bm:Map.Children> <!-- Data Layer--> <bm:MapLayer Name="Data

我有以下代码:

DataLayer.Children.Clear();
…从设计时映射层中删除图钉,声明如下:

<bm:Map Credentials="CuellarDobsonMcNallyPalmer420s" x:Name="photraxMap" Margin="0,0,0,0" >
    <bm:Map.Children>
        <!-- Data Layer-->
        <bm:MapLayer Name="DataLayer"/>
        . . .
// App.xaml.cs
sealed partial class App : Application
{
    public static List<MapLayer> dynamicMapLayers  { get; set; } 

    . . .

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
     . . .
        dynamicMapLayers = new List<MapLayer>();

//MainPage.xaml.cs (update of previously shown code):
MapLayer mapLayerToUse = DataLayer;
if (Cre8DynamicMapLayer)
{
    mapLayerToUse = new MapLayer();
    mapLayerToUse.Name = String.Empty;
    dynamicMapLayers.Add(mapLayerToUse); // <= this is the new line
    photraxMap.Children.Insert(0, mapLayerToUse);
}

// Finally, insted of doing all that looping as shown in my proposed answer, simply do this instead:
foreach (MapLayer ml in dynamicMapLayers)
{
    ml.Children.Clear();
}
…那么我如何才能清除这些动态创建的贴图层呢

我可以做一些相关的事情,比如给动态地图层随机命名,将它们存储在一个列表中,然后在该列表上迭代,调用Children.Clear(在找到具有该名称的地图层后),但我认为肯定有一种更简单、更优雅的方法。有人知道吗

更新 尼古拉斯,是这样的:

<bm:Map Credentials="CuellarDobsonMcNallyPalmer420s" x:Name="photraxMap" Margin="0,0,0,0" >
    <bm:Map.Children>
        <!-- Data Layer-->
        <bm:MapLayer Name="DataLayer"/>
        . . .
// App.xaml.cs
sealed partial class App : Application
{
    public static List<MapLayer> dynamicMapLayers  { get; set; } 

    . . .

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
     . . .
        dynamicMapLayers = new List<MapLayer>();

//MainPage.xaml.cs (update of previously shown code):
MapLayer mapLayerToUse = DataLayer;
if (Cre8DynamicMapLayer)
{
    mapLayerToUse = new MapLayer();
    mapLayerToUse.Name = String.Empty;
    dynamicMapLayers.Add(mapLayerToUse); // <= this is the new line
    photraxMap.Children.Insert(0, mapLayerToUse);
}

// Finally, insted of doing all that looping as shown in my proposed answer, simply do this instead:
foreach (MapLayer ml in dynamicMapLayers)
{
    ml.Children.Clear();
}
//App.xaml.cs
密封部分类应用程序:应用程序
{
公共静态列表{get;set;}
. . .
仅启动受保护的覆盖无效(启动ActivatedEventArgs e)
{
. . .
dynamicMapLayers=新列表();
//MainPage.xaml.cs(更新先前显示的代码):
MapLayer mapLayerToUse=数据层;
如果(Cre8DynamicMapLayer)
{
mapLayerToUse=新的MapLayer();
mapLayerToUse.Name=String.Empty;

dynamicMapLayers.Add(mapLayerToUse);//我希望会有一些非常简洁和精辟的东西,但可能毕竟我不得不这样做:

// Adapted the following from http://www.codeease.com/only-remove-pushpins-from-a-layer-in-bing-map.html
private void ClearPushpins()
{
    List<UIElement> elementsToRemove = new List<UIElement>();
    List<UIElement> pushpinToRemove = new List<UIElement>();
    foreach (String photoset in App.CurrentlyMappedPhotosets)
    {
        foreach (UIElement element in photraxMap.Children)
        {
            if (element.GetType() == typeof(MapLayer))
            {
                MapLayer Lay = (MapLayer)element;
                if (Lay.Name == photoset)
                {
                    foreach (UIElement p in Lay.Children)
                    {
                        if (p.GetType() == typeof(Pushpin))
                        {
                            pushpinToRemove.Add(p);
                        }
                    }
                    foreach (UIElement pin in pushpinToRemove)
                    {
                        Lay.Children.Remove(pin);

                    }
                    elementsToRemove.Add(Lay);
                }
            }
            foreach (UIElement e in elementsToRemove)
            {
                photraxMap.Children.Remove(e);
            }
        }
    }
}
//改编自http://www.codeease.com/only-remove-pushpins-from-a-layer-in-bing-map.html
专用图钉()
{
List elementsToRemove=新建列表();
List pushpinToRemove=新列表();
foreach(应用程序CurrentlyMappedPhotoset中的字符串photoset)
{
foreach(photraxMap.Children中的UIElement)
{
if(element.GetType()==typeof(MapLayer))
{
MapLayer层=(MapLayer)元素;
如果(Lay.Name==photoset)
{
foreach(非专业儿童中的UIP元素)
{
if(p.GetType()==typeof(图钉))
{
推杆移动。添加(p);
}
}
foreach(推杆中的UIE元件销拆卸)
{
放置。儿童。移除(销);
}
elementsToRemove.Add(Lay);
}
}
foreach(elementsToRemove中的UIElement e)
{
photraxMap.Children.Remove(e);
}
}
}
}

我希望能有一些非常简洁和精辟的东西,但也许我终究要做这样的事情:

// Adapted the following from http://www.codeease.com/only-remove-pushpins-from-a-layer-in-bing-map.html
private void ClearPushpins()
{
    List<UIElement> elementsToRemove = new List<UIElement>();
    List<UIElement> pushpinToRemove = new List<UIElement>();
    foreach (String photoset in App.CurrentlyMappedPhotosets)
    {
        foreach (UIElement element in photraxMap.Children)
        {
            if (element.GetType() == typeof(MapLayer))
            {
                MapLayer Lay = (MapLayer)element;
                if (Lay.Name == photoset)
                {
                    foreach (UIElement p in Lay.Children)
                    {
                        if (p.GetType() == typeof(Pushpin))
                        {
                            pushpinToRemove.Add(p);
                        }
                    }
                    foreach (UIElement pin in pushpinToRemove)
                    {
                        Lay.Children.Remove(pin);

                    }
                    elementsToRemove.Add(Lay);
                }
            }
            foreach (UIElement e in elementsToRemove)
            {
                photraxMap.Children.Remove(e);
            }
        }
    }
}
//改编自http://www.codeease.com/only-remove-pushpins-from-a-layer-in-bing-map.html
专用图钉()
{
List elementsToRemove=新建列表();
List pushpinToRemove=新列表();
foreach(应用程序CurrentlyMappedPhotoset中的字符串photoset)
{
foreach(photraxMap.Children中的UIElement)
{
if(element.GetType()==typeof(MapLayer))
{
MapLayer层=(MapLayer)元素;
如果(Lay.Name==photoset)
{
foreach(非专业儿童中的UIP元素)
{
if(p.GetType()==typeof(图钉))
{
推杆移动。添加(p);
}
}
foreach(推杆中的UIE元件销拆卸)
{
放置。儿童。移除(销);
}
elementsToRemove.Add(Lay);
}
}
foreach(elementsToRemove中的UIElement e)
{
photraxMap.Children.Remove(e);
}
}
}
}

我希望能有一些非常简洁和精辟的东西,但也许我终究要做这样的事情:

// Adapted the following from http://www.codeease.com/only-remove-pushpins-from-a-layer-in-bing-map.html
private void ClearPushpins()
{
    List<UIElement> elementsToRemove = new List<UIElement>();
    List<UIElement> pushpinToRemove = new List<UIElement>();
    foreach (String photoset in App.CurrentlyMappedPhotosets)
    {
        foreach (UIElement element in photraxMap.Children)
        {
            if (element.GetType() == typeof(MapLayer))
            {
                MapLayer Lay = (MapLayer)element;
                if (Lay.Name == photoset)
                {
                    foreach (UIElement p in Lay.Children)
                    {
                        if (p.GetType() == typeof(Pushpin))
                        {
                            pushpinToRemove.Add(p);
                        }
                    }
                    foreach (UIElement pin in pushpinToRemove)
                    {
                        Lay.Children.Remove(pin);

                    }
                    elementsToRemove.Add(Lay);
                }
            }
            foreach (UIElement e in elementsToRemove)
            {
                photraxMap.Children.Remove(e);
            }
        }
    }
}
//改编自http://www.codeease.com/only-remove-pushpins-from-a-layer-in-bing-map.html
专用图钉()
{
List elementsToRemove=新建列表();
List pushpinToRemove=新列表();
foreach(应用程序CurrentlyMappedPhotoset中的字符串photoset)
{
foreach(photraxMap.Children中的UIElement)
{
if(element.GetType()==typeof(MapLayer))
{
MapLayer层=(MapLayer)元素;
如果(Lay.Name==photoset)
{
foreach(非专业儿童中的UIP元素)
{
if(p.GetType()==typeof(图钉))
{
推杆移动。添加(p);
}
}
foreach(推杆中的UIE元件销拆卸)
{
放置。儿童。移除(销);
}
elementsToRemove.Add(Lay);
}
}
foreach(elementsToRemove中的UIElement e)
{
photraxMap.Children.Remove(e);
}
}
}
}

我希望能有一些非常简洁和精辟的东西,但也许我终究要做这样的事情:

// Adapted the following from http://www.codeease.com/only-remove-pushpins-from-a-layer-in-bing-map.html
private void ClearPushpins()
{
    List<UIElement> elementsToRemove = new List<UIElement>();
    List<UIElement> pushpinToRemove = new List<UIElement>();
    foreach (String photoset in App.CurrentlyMappedPhotosets)
    {
        foreach (UIElement element in photraxMap.Children)
        {
            if (element.GetType() == typeof(MapLayer))
            {
                MapLayer Lay = (MapLayer)element;
                if (Lay.Name == photoset)
                {
                    foreach (UIElement p in Lay.Children)
                    {
                        if (p.GetType() == typeof(Pushpin))
                        {
                            pushpinToRemove.Add(p);
                        }
                    }
                    foreach (UIElement pin in pushpinToRemove)
                    {
                        Lay.Children.Remove(pin);

                    }
                    elementsToRemove.Add(Lay);
                }
            }
            foreach (UIElement e in elementsToRemove)
            {
                photraxMap.Children.Remove(e);
            }
        }
    }
}
//改编自http://www.codeease.com/only-remove-pushpins-from-a-layer-in-bing-map.html
专用图钉()
{
List elementsToRemove=新建列表();
List pushpinToRemove=新列表();
foreach(应用程序CurrentlyMappedPhotoset中的字符串photoset)
{
foreach(photraxMap.Children中的UIElement)
{
if(element.GetType()==typeof(MapLayer))
{
MapLayer层=(MapLayer)元素;
如果(Lay.Name==photoset)
{
foreach(非专业儿童中的UIP元素)
{
if(p.GetType()==typeof(图钉))
{
推杆移动。添加(p);