Click or drag to resize
Code Example - ArcGISDynamicMapServiceLayer_VisibleLayers

Shows obtaining which sub-layers are visible using the ArcGISDynamicMapServiceLayer.GetAllDetailsAsync Method. Then the user can interact with application and change the VisibleLayers of the ArcGISDynamicMapServiceLayer.

Code Example
ArcGISDynamic Map Service Layer Visible Layers

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

<Window x:Class="ArcGISDynamicMapServiceLayer_VisibleLayers.MainWindow"
    Title="MainWindow" Height="600" Width="800">

    <Grid x:Name="LayoutRoot">

        <!-- Add a MapView Control. -->
        <esri:MapView x:Name="MapView1" Background="White" HorizontalAlignment="Left" Margin="10,177,0,0"  VerticalAlignment="Top" 
                   Height="383" Width="772">

            <!-- Add a Map. -->
            <esri:Map x:Name="Map1">

                <!-- Add an ArcGISDynamicMapServiceLayer. -->
                <esri:ArcGISDynamicMapServiceLayer ID="ESRI_StateCityHighway_USA"


        <!-- Add a Button to apply to modify ArcGISDynamicMapServiceLayer.VisibleLayers. -->
        <Button Content="Set VisibleLayers" Height="105" HorizontalAlignment="Left" Margin="10,67,0,0" 
          Name="Button_SetVisibleLayers" VerticalAlignment="Top" Width="184" Click="Button_SetVisibleLayers_Click"/>

        <!-- Add a ListBox to display all of the VisibleLayers for each sub-layer. Embedded in each row of
        the ListBox will be a CheckBoxes (one for each sub-layer) that user can make changes to and then click the
        apply button.-->
        <ListBox Height="105" HorizontalAlignment="Left" Margin="199,67,0,0" Name="ListBox1" VerticalAlignment="Top" 
           Width="583" />

        <!-- Add a TextBlock to hold the instructions on how to use the sample code. -->
        <TextBlock Height="62" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="772" 
                   TextWrapping="Wrap" Margin="10,0,0,0" />


SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "ArcGISDynamicMapServiceLayer_VisibleLayers.MainWindow" to be just "MainWindow".

namespace ArcGISDynamicMapServiceLayer_VisibleLayers
    public partial class MainWindow : System.Windows.Window
        public MainWindow()
            // This call is required by the designer.

            // Add any initialization after the InitializeComponent() call.

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.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. Check on/off the various CheckBoxes and then click the Button to modify the " + 
                              "ArcGISDynamicMapServiceLayer.VisibleLayers Property to change the viewing of the sub-layers.";

            // Zoom to a specified target area in the in the Map.
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(-89.4581767708459, 30.1713047548993, -79.646568737671, 36.0582695748043);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myEnvelope);

            // Call the asynchronous function to populate the CheckBoxes with the visibility of the sub-layers.

        private async void Init()
            // Get the ArcGISDynamicMapServiceLayer from the Map.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer)(MapView1.Map.Layers[0]);

            // Get the AllLayersServiceInfo object via the ArcGISDynamicMapServiceLayer.GetAllDetailsAsync Method.
            Esri.ArcGISRuntime.ArcGISServices.AllLayersServiceInfo myAllLayersServiceInfo = await myArcGISDynamicMapServiceLayer.GetAllDetailsAsync();

            // Get the collection of FeatureServiceLayerInfo objects for the layers in the AllLayersServiceInfo object.
            System.Collections.Generic.IReadOnlyList<Esri.ArcGISRuntime.ArcGISServices.FeatureServiceLayerInfo> myIReadOnlyList = myAllLayersServiceInfo.Layers;

            // Loop through each FeatureServiceLayerInfo object.
            foreach (Esri.ArcGISRuntime.ArcGISServices.FeatureServiceLayerInfo oneFeatureServiceLayerInfo in myIReadOnlyList)
                // Get the ID, Name, and default visibility of the sub-layer.
                int myId = oneFeatureServiceLayerInfo.ID;
                string myName = oneFeatureServiceLayerInfo.Name;
                bool myVisibility = oneFeatureServiceLayerInfo.DefaultVisibility;

                // Create a StackPanel to hold multiple Framework Element Controls. 
                System.Windows.Controls.StackPanel myStackPanel = new System.Windows.Controls.StackPanel();
                myStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;

                // Create a TextBlock to hold the sub-layer visibility information.
                System.Windows.Controls.TextBlock mytextBlock2 = new System.Windows.Controls.TextBlock();
                mytextBlock2.Text = "Visible: ";

                // Create a CheckBox to allow the user to interact with the visibility of the sub-layer. 
                // Note: the sub-layer ID is embedded as the .Tag for the CheckBox. This will be useful to construct the 
                // ObservableCollection(Of Integer) values in setting the ArcGISDynamicMapServiceLayer.VisibleLayers
                // Property later in the code example.
                System.Windows.Controls.CheckBox myCheckBox = new System.Windows.Controls.CheckBox();
                myCheckBox.IsChecked = myVisibility;
                myCheckBox.Tag = myId;

                // Create a TextBlock to hold information about the sub-layer ID and Name.
                System.Windows.Controls.TextBlock myTextBlock = new System.Windows.Controls.TextBlock();
                myTextBlock.Text = "  LayerID: " + myId.ToString() + " (" + myName + ")";

                // Add the StackPanel into the ListBox.

        private void Button_SetVisibleLayers_Click(object sender, System.Windows.RoutedEventArgs e)
            // Get the ArcGISDynamicMapServiceLayer from the MapView Control by its ID value that was set in XAML.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer)(MapView1.Map.Layers["ESRI_StateCityHighway_USA"]);

            // Create an ObservableCollection of sub-layer ID's (Integer) that will their visibility turned on. 
            System.Collections.ObjectModel.ObservableCollection<int> myObservableCollection = new System.Collections.ObjectModel.ObservableCollection<int>();

            // Loop through each StackPanel in the ListBox.
            foreach (System.Windows.Controls.StackPanel myStackPanel in ListBox1.Items)
                // Get the CheckBox which holds the sub-layer ID and visibility values.
                System.Windows.Controls.CheckBox myCheckBox = (System.Windows.Controls.CheckBox)(myStackPanel.Children[1]);
                int myTag = (int)myCheckBox.Tag; // This is the sub-layer ID!

                // Only add those sub-layers into the ObservableCollection that we want to be visible.
                if (myCheckBox.IsChecked == true)

            // Apply the custom ObservableCollection of layer ID values to the ArcGISDynamicMapServiceLayer.VisibleLayers Property.
            myArcGISDynamicMapServiceLayer.VisibleLayers = myObservableCollection;