1、功能介绍
This sample contains a Map with one tiled map service layer and one dynamic map service layer. A layer list shows the layers (sublayers) within the dynamic map service and a visibility check box. Element binding in XAML is used to populate the ListBox contents and enable runtime interactivity between sublayers and UI elements in the layer list.
展示一个dynamic map service 下的各个图层
2、代码详解
MainPage.axml中
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:esri="http://schemas.esri.com/arcgis/client/2009">
<Grid x:Name="LayoutRoot" Background="White">
<esri:Map x:Name="MyMap" Extent="-121,32,-113,36">
<esri:ArcGISTiledMapServiceLayer ID="Street Map"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_ShadedRelief_World_2D/MapServer" />
<esri:ArcGISDynamicMapServiceLayer ID="DynamicLayerCalifornia"
Url="http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer"
Initialized="ArcGISDynamicMapServiceLayer_Initialized" /> //DynamicLayerCalifornia图层初始化时调用ArcGISDynamicMapServiceLayer_Initialized函数
</esri:Map>
<Border Background="#CC5C90B2" BorderThickness="1" CornerRadius="5"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="20,20,20,30" Padding="10" BorderBrush="Black">
<Border.Effect>
<DropShadowEffect/> //边框的阴影效果
<Grid> //定义一个grid
<Grid.RowDefinitions> //grid中行定义,以下包含两行
<RowDefinition Height="15" /> //第一行,行高15
<RowDefinition Height="*" /> //第二行,行高“*“,解释见下面部分
</Grid.RowDefinitions>
<TextBlock Text="Layers in California service" FontWeight="Bold" Grid.Row="0" /> //grid第一行,包括一个TextBlock
<ListBox Margin="0,5,0,0" ItemsSource="{Binding ElementName=MyMap, Path=Layers.[1].Layers}"
Grid.Row="1"> //grid第二行,包括一个ListBox,绑定MyMap的Layers.[1].Layers,即DynamicLayerCalifornia的Layers
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Margin="2" //listbox里面包括一个CheckBox,以下为其属性
Name="DynamicLayerCalifornia" //名称
Content="{Binding Name}" //绑定图层的Name属性
IsChecked="{Binding DefaultVisibility}" //是否选中绑定图层的可视情况
Tag="{Binding ID}" //标签绑定ID
ClickMode="Press"
Click="CheckBox_Click" /> //click触发CheckBox_Click函数
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Border>
</Grid>
</UserControl>
RowDefinition Height有三种方式:
<RowDefinition Height="doubleValue"/> The row's height, expressed as a floating-point value for a pixel count
- or -
<RowDefinition Height="starSizing"/> (*)size rows or columns to take the remaining available space in a Grid
-or-
<RowDefinition Height="Auto"/> The row's height, described by the literal Auto
MainPage.axml.cs中
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
//控制地图图层显示,获取CheckBox对应的图层ID,判断是否显示,存储到visibleLayerList,并将结果返回到dynamicServiceLayer.VisibleLayers,即可控制图层显示/不显示
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox tickedCheckBox = sender as CheckBox;
string serviceName = tickedCheckBox.Name;
bool visible = (bool)tickedCheckBox.IsChecked;
int layerIndex = (int)tickedCheckBox.Tag;
ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer = MyMap.Layers[serviceName] as
ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
List<int> visibleLayerList =
dynamicServiceLayer.VisibleLayers != null
? dynamicServiceLayer.VisibleLayers.ToList() : new List<int>();
if (visible)
{
if (!visibleLayerList.Contains(layerIndex))
visibleLayerList.Add(layerIndex);
}
else
{
if (visibleLayerList.Contains(layerIndex))
visibleLayerList.Remove(layerIndex);
}
dynamicServiceLayer.VisibleLayers = visibleLayerList.ToArray();
}
//获取ArcGISDynamicMapServiceLayer地图服务初始化时,设定地图的dynamicServiceLayer.VisibleLayers
{
ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicServiceLayer =
sender as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
if (dynamicServiceLayer.VisibleLayers == null)
dynamicServiceLayer.VisibleLayers = GetDefaultVisibleLayers(dynamicServiceLayer);
}
//获取ArcGISDynamicMapServiceLayer服务提供的地图中的可视图层,返回一个可视图层的ID队列
private int[] GetDefaultVisibleLayers(ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dynamicService)
{
List<int> visibleLayerIDList = new List<int>();
ESRI.ArcGIS.Client.LayerInfo[] layerInfoArray = dynamicService.Layers;
for (int index = 0; index < layerInfoArray.Length; index++)
{
if (layerInfoArray[index].DefaultVisibility)
visibleLayerIDList.Add(index);
}
return visibleLayerIDList.ToArray();
}
}
}
3、界面见http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SubLayerList