1、功能
This sample includes a Map with one tiled map service layer and two dynamic map service layers. A layer list shows the layer (service) name, a visibility check box, and a slider to adjust opacity. Element binding in XAML is used to populate the ListBox contents and enable runtime interactivity between layers and UI elements in the layer list.
用个list显示不同图层,并可以显示图层名称,修改图层的透明度等,本例中,将一个服务(mxd文件)当成一个图层,和传统的图层(lyr)性质不同,传统图层的例子见SubLayer list
2、代码详解
MainPage.axml中
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="-120,20,-90,60" >
<esri:ArcGISTiledMapServiceLayer ID="Street Map"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_ShadedRelief_World_2D/MapServer"/>
<esri:ArcGISDynamicMapServiceLayer ID="State,City,Highway" Opacity="0.6"
Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer"/>
<esri:ArcGISDynamicMapServiceLayer ID="California" Opacity="0.4"
Url="http://serverapps.esri.com/ArcGIS/rest/services/California/MapServer" />
</esri:Map>
<Border Background="#996495ED" BorderThickness="1" CornerRadius="5"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="20" Padding="5" BorderBrush="Black" >
<ListBox x:Name="MyList" ItemsSource="{Binding ElementName=MyMap, Path=Layers}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!--Layer visibility checkbox-->
<CheckBox IsChecked="{Binding Visible, Mode=TwoWay}" />
<!--Opacity slider-->
<Slider Margin="-5,0,0,0" Minimum="0" Maximum="1" Width="30"
Value="{Binding Opacity, Mode=TwoWay}" Height="18" />
<!--Layer name-->
<TextBlock Text="{Binding ID, Mode=OneWay}" Margin="5,0,0,0" >
<!-- Tooltip on hover-->
<ToolTipService.ToolTip>
<StackPanel MaxWidth="400">
<TextBlock FontWeight="Bold" Text="{Binding CopyrightText}" TextWrapping="Wrap" />
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
</StackPanel>
</ToolTipService.ToolTip>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>
</Grid>
</UserControl>
<Border Background="#996495ED" BorderThickness="1" CornerRadius="5"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="20" Padding="5" BorderBrush="Black" >
设置ListBox的底色,边框,文字排版,位置等
<ListBox x:Name="MyList" ItemsSource="{Binding ElementName=MyMap, Path=Layers}">
设置ListBox的名称,数据源(MyMap的layers),绑定MyMap的Layers
<StackPanel Orientation="Horizontal"> 排版方向,横向(即下面的checkbox,Slider,TextBlock横向排列)
<!--Layer visibility checkbox-->
<CheckBox IsChecked="{Binding Visible, Mode=TwoWay}" />
<!--Opacity slider-->
<Slider Margin="-5,0,0,0" Minimum="0" Maximum="1" Width="30"
Value="{Binding Opacity, Mode=TwoWay}" Height="18" />
<!--Layer name-->
<TextBlock Text="{Binding ID, Mode=OneWay}" Margin="5,0,0,0" >
以CheckBox为例,<CheckBox IsChecked="{Binding Visible, Mode=TwoWay}" />
Binding Visible:绑定地图图层的Visible属性,Mode=TwoWay,绑定模式解释如下:
You use the Mode property to specify the direction of the binding. The following enumeration list shows the available options for binding updates:
· TwoWay updates the target property or the property whenever either the target property or the source property changes.
· OneWay updates the target property only when the source property changes.
· OneTime updates the target property only when the application starts or when the DataContext undergoes a change.
· OneWayToSource updates the source property when the target property changes.
· Default causes the default Mode value of target property to be used.
设置Tooltip on hover,当鼠标放到layerlist上的图层名称上时,显示内容
<ToolTipService.ToolTip>
<StackPanel MaxWidth="400">排版模式,最宽400
<TextBlock FontWeight="Bold" Text="{Binding CopyrightText}" TextWrapping="Wrap" /> TextBlock FontWeight设置,绑定图层的CopyrightText属性
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />TextBlock设置,绑定图层的Description属性
</StackPanel>
</ToolTipService.ToolTip>
3.、效果
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#LayerList