ArcGIS API for Silverlight - Library Reference
VisibleLayers Property
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client Namespace > ArcGISDynamicMapServiceLayer Class : VisibleLayers Property

Gets or sets the sub-layers that are visible in an ArcGISDynamicMapServiceLayer.

Syntax

Visual Basic (Declaration) 
Public Property VisibleLayers As Integer()
C# 
public int[] VisibleLayers {get; set;}

Remarks

To set visiblility of an ArcGISDynamicMapServiceLayer when the client application first loads, define the VisibleLayers Property during the code-behind Initialized Event or any time after the Initialization occurs.

Do not use the VisibleLayers Property to try and determine which sub-layers are visible for the ArcGISDynamicMapServiceLayer web service. The VisibleLayers Property will always return null/Nothing for the web service. Once the VisibleLayers Property has been set you can then you can use the get/Read operations of the VisibleLayer Property to obtain valid results. Use the ArcGISDynamicMapServiceLayer.GetLayerVisibility Method to return a boolean for a specific sub-layer ID of an ArcGISDynamicMapServiceLayer telling whether it is visible (true) or not (false) in the web service.

Another option for determining if the sub-layer of an ArcGISDynamicMapServiceLayer is visible or not is to enter the Url for the sub-layer in the address bar of an internet browser and look for the Default Visibility section in the ArcGIS REST Services Directory. Remember the sub-layer has the same Url as the main ArcGISDynamicMapServiceLayer but with a forward slash and the sub-layer ID value appended to the Url (ex: http://server.arcgisonline.com/ArcGIS/rest/services/Reference/ESRI_BoundariesPlaces_World_2D/MapServer/0). See the following screen shot:

Determining the sub-layer visibility using the ArcGIS REST Services Directory.

NOTE: Obtaining information (i.e. get/Read) for the various Properties of an ArcGISDynamicMapServiceLayer should occur in the Initialized Event or any time after the Initialized Event occurs. This ensures that information retrieved about the ArcGISDynamicMapServiceLayer has been obtained after a complete round trip from ArcGIS Server. Do not be tempted to try and access ArcGISDynamicMapServiceLayer Property information from generic application Events like: MainPage.Loaded or the Constructor, etc. as the ArcGISDynamicMapServiceLayer has not been Initialized and erroneous information will be returned. Likewise, ArcGISDynamicMapServiceLayer Methods should not be invoked until after the ArcGISDynamicMapServiceLayer Initialized Event has fired or from within the Initialized Event to avoid erroneous results.

KNOWN ISSUE: As of Visual Studio 10 (including SP1), integer arrays do not work as expected in XAML for Silverlight. Using the following sample XAML code fragment for an ArcGISDynamicMapServiceLayer, the VisibleLayers Property causes a blue squiggly line to appear in the XAML for the VisibleLayer Property:

<esri:ArcGISDynamicMapServiceLayer ID="UTAH"
Url="http://dk-red2a:6080/arcgis/rest/services/UTAH/MapServer"
VisibleLayers="1,2" />

Which results in the error:
Unable to cast object of type 'Microsoft.Expression.DesignModel.DocumentModel.DocumentPrimitiveNode' to type 'Microsoft.Expression.DesignModel.DocumentModel.DocumentCompositeNode'.

Performing a re-build of the application which causes a refresh of the Design view results in the error message:
InvalidCastException was thrown on "ArcGISDynamicMapServiceLayer": Unable to cast object of type 'Microsoft.Expression.DesignModel.DocumentModel.DocumentPrimitiveNode' to type 'Microsoft.Expression.DesignModel.DocumentModel.DocumentCompositeNode'.
at Microsoft.Expression.DesignModel.InstanceBuilders.ArrayInstanceBuilder.InstantiateTargetType(IInstanceBuilderContext context, ViewNode viewNode)
at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.Instantiate(IInstanceBuilderContext context, ViewNode viewNode)
at Microsoft.Expression.DesignModel.Core.ViewNodeManager.Instantiate(ViewNode viewNode)

See the following screen shot:

Visual Studio interger array XAML limitation issue.

Although you can run the application, this error locks up the Visual Studio 2010 Design tab for the .xaml page. It is recommended that developers use the ArcGISDynamicMapServiceLayer.VisibleLayers Property only in the code-behind until Microsoft addresses this issue.

The following is a list of all ArcGIS Silverlight API types for which this issue occurs:

  • ESRI.ArcGIS.Client.Editor.LayerIDs Property
  • ESRI.ArcGIS.Client.ArcGISImageServiceLayer.BandIDs Property
  • ESRI.ArcGIS.Client.ArcGISImageServiceLayer.BandIDs Property
  • ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer.VisibleLayers Property
  • ESRI.ArcGIS.Client.Toolkit.TemplatePicker.LayerIDs Property
  • ESRI.ArcGIS.Client.Toolkit.EditorWidget.LayerIDs Property
  • ESRI.ArcGIS.Client.Toolkit.DataSources.WMSLayer.Layers Property
  • ESRI.ArcGIS.Client.Toolkit.Legend.LayerIDs
  • ESRI.ArcGIS.Client.QueryDataSource.OIDFields Property

Property Value

Integer array containing sub-layer ids.

Example

How to use:

In this example the there are three sub-layers (IDs: 0, 1, and 2) in the ArcGISDynamicMapServiceLayer. They are all visible (i.e. turned on) by default in the ArcGIS Server web service. We are going to override the default and only show the second and third sub layers (i.e. 1 and 2) by using the ArcGISDynamicMapServiceLayer.VisibleLayers Property in the Initialized Event.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Modifying the visible sub-layers when an ArcGISDynamicMapServiceLayer Initializes.

XAMLCopy Code
<Grid x:Name="LayoutRoot">
  
  <!-- Add a Map Control and zoom into an area of interest that shows useful information. -->
  <esri:Map Background="White" HorizontalAlignment="Left" Margin="12,168,0,0" Name="Map1" VerticalAlignment="Top" 
               Height="300" Width="616" Extent="-89.4581767708459,30.1713047548993,-79.646568737671,36.0582695748043">
  
    <!-- Add an ArcGISDynamicMapServiceLayer. Wire up an Initialized Event to set the visibility for the 
       individual sub-layers to a custom setting. -->
    <esri:ArcGISDynamicMapServiceLayer ID="ESRI_StateCityHighway_USA"
        Url="http://sampleserver1.arcgisonline.com/arcgis/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer" 
        Initialized="ArcGISDynamicMapServiceLayer_Initialized"/>
  
  </esri:Map>
  
  <!-- Provide some GUI instructions. -->
  <sdk:Label Height="28" HorizontalAlignment="Left" Margin="12,142,0,0" Name="Label1" VerticalAlignment="Top" Width="616" 
              Content="Only the 2nd and 3rd sub-layers are visible because we used the VisibleLayers Property."/>
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="81" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="640" TextWrapping="Wrap" 
       Text="In this example the there are three sub-layers (IDs: 0, 1, and 2) in the ArcGISDynamicMapServiceLayer. 
             They are all visible (i.e. turned on) by default in the ArcGIS Server web service. We are going to 
             override the default and only show the second and third sub layers (i.e. 1 and 2) by using the
             ArcGISDynamicMapServiceLayer.VisibleLayers Property in the Initialized Event." />
</Grid>
C#Copy Code
private void ArcGISDynamicMapServiceLayer_Initialized(object sender, System.EventArgs e)
{
  // This function will draw specific sub-layers using the ArcGISDynamicMapServiceLayer.VisibleLayers Property.
  
  // Get the ArcGISDynamicMapServiceLayer from the Map Control based upon its position in the Control.
  ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)(Map1.Layers[0]);
  
  // In this example the there are three sub-layers (IDs: 0, 1, and 2) and they are all visible (i.e. turned on) 
  // by default in the ArcGIS Server web service. We are going to override the default and only show the second 
  // and third sub layers (i.e. 1 and 2). 
  int[] myVisibleLayers = {1, 2};
  
  // Use the VisibleLayers Property to control which sub-layers are drawn.
  myArcGISDynamicMapServiceLayer.VisibleLayers = myVisibleLayers;
}
VB.NETCopy Code
Private Sub ArcGISDynamicMapServiceLayer_Initialized(sender As System.Object, e As System.EventArgs)
  
  ' This function will draw specific sub-layers using the ArcGISDynamicMapServiceLayer.VisibleLayers Property.
  
  ' Get the ArcGISDynamicMapServiceLayer from the Map Control based upon its position in the Control.
  Dim myArcGISDynamicMapServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer = CType(Map1.Layers.Item(0), ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
  
  ' In this example the there are three sub-layers (IDs: 0, 1, and 2) and they are all visible (i.e. turned on) 
  ' by default in the ArcGIS Server web service. We are going to override the default and only show the second 
  ' and third sub layers (i.e. 1 and 2). 
  Dim myVisibleLayers() As Integer = {1, 2}
  
  ' Use the VisibleLayers Property to control which sub-layers are drawn.
  myArcGISDynamicMapServiceLayer.VisibleLayers = myVisibleLayers
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.