Click or drag to resize
Code Example - LayerDefinitionsSimple

Demonstrates setting the LayerDefinitions Property of an ArcGISDynamicMapServiceLayer where two sub-layers are present. One sub-layer has the LayerDefinition set in XAML and the other sub-layer has it's LayerDefinition set via code-behind.

Code Example
Layer Definitions Simple

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="LayerDefinitionsSimple.MainWindow"
    Title="MainWindow" Height="600" Width="800">
    <Grid x:Name="LayoutRoot">

        <!-- Add a MapView Control. -->
        <esri:MapView x:Name="MapView1" Background="White" Margin="0,75,0,0" HorizontalAlignment="Left" Height="424" 
                      Width="782" VerticalAlignment="Top">

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

                <!-- Add a background ArcGISDynamicMapServiceLayer. -->
                <esri:ArcGISDynamicMapServiceLayer ID="World_Light_Gray_Base" ImageFormat="PNG24"
                    ServiceUri="" />

                Add another ArcGISDynamicMapServiceLayer. NOTE: This ArcGISDynamicMapServiceLayer has two sub-layers (Wells(0) 
                and Fields(1)). We will only set the LayerDefinitions for the Fields(1) sub-layer. The other sub-layer (Wells(0))
                will have it's Visibility turned off. In the code-behind, the other sub-layer (Wells(0)) will have it's
                'LayerDefinitions set.
                <esri:ArcGISDynamicMapServiceLayer ID="OilFields" VisibleLayers="1" 
                        <esri:LayerDefinition LayerID="1" Definition="STATUS='Abandoned'" />


        <!-- Add controls for the user to set the ArcGISDynamicMapServiceLayer.LayerDefinitions via code behind. -->
        <Button Content="Add another LayerDefinitions via code-behind" Height="56" HorizontalAlignment="Left" Margin="10,504,0,0" 
          Name="Button_LayerDefinitions" VerticalAlignment="Top" Width="772" Click="Button_LayerDefinitions_Click"/>

        <!-- TextBlock to hold the instructions on how to use the sample code. -->
        <TextBlock Height="70" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="782" 
             TextWrapping="Wrap" />


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 "LayerDefinitionsSimple.MainWindow" to be just "MainWindow".

namespace LayerDefinitionsSimple
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
        public MainWindow()

            // Add instructions on how to use the sample.  
            string instructionsText = "When the application loads, an ArcGISDynamicMapServiceLayer will be added to " + 
                                      "the Map via XAML with it's LayerDefintions set for the second sub-layer (Fields(1)). The first sub-layer " + 
                                      "(Wells(0)) will not be shown because it's visibility will be turned off. Then click the button to add another " + 
                                      "ArcGISDynamicMapServiceLayer (this time via code-behind) using the same ServiceUri endpoint. This time the " + 
                                      "LayerDefinitions will be set for the first sub-layer (Wells(0)) and the second sub-layer (Fields(1) will have " + 
                                      "it's visibility turned off.";
            TextBlock1.Text = instructionsText;

            // Set the initial extent of the Map (via the InitialViewpoint Property) to be a small region in northern Kansas.
            Esri.ArcGISRuntime.Geometry.Envelope myExtent;
            myExtent = new Esri.ArcGISRuntime.Geometry.Envelope(-10571409.0775969, 4736027.28190545, -10532273.3126952, 4752942.74039235);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myExtent);

        private void Button_LayerDefinitions_Click(object sender, System.Windows.RoutedEventArgs e)

            // NOTE: This ArcGISDynamicMapServiceLayer has two sub-layers (Wells(0) and Fields(1)).

            // Create a new ArcGISDynamicMapServiceLayer. Set the .ServiceUri and .ID Properties.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = null;
            myArcGISDynamicMapServiceLayer = new Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer();
            myArcGISDynamicMapServiceLayer.ServiceUri = "";
            myArcGISDynamicMapServiceLayer.ID = "Wells";

            // Create a new LayerDefinition.
            Esri.ArcGISRuntime.Layers.LayerDefinition myDefinition = new Esri.ArcGISRuntime.Layers.LayerDefinition();

            // Set the LayerID Property of the LayerDefinition (this will be for the Wells(0) sub-layer).
            myDefinition.LayerID = 0;

            // Specify the Definition Property of the LayerDefinition. This is essentially the same as a Where clause. This will
            // restrict the number of feature that are returned in the layer.
            myDefinition.Definition = "WELL_CLASS='Plugged and Abandoned'";

            // Create an ObservableCollection and add the LayerDefinition to it.
            System.Collections.ObjectModel.ObservableCollection<Esri.ArcGISRuntime.Layers.LayerDefinition> myObservableCollection = null;
            myObservableCollection = new System.Collections.ObjectModel.ObservableCollection<Esri.ArcGISRuntime.Layers.LayerDefinition>();

            // Apply the custom LayerDefinition to the LayerDefinitions ObservableCollection. 
            myArcGISDynamicMapServiceLayer.LayerDefinitions = myObservableCollection;

            // NOTE: Because the ArcGISDynamicMapServiceLayer has two sub-layers (Wells(0) and Fields(1)) we need to address the issue
            // of the other sub-layer (Fields(1)). Because the (Fields(1)) sub-layer is being drawn via the XAML, we will just hide the 
            // Fields(1) sub-layer from drawing.
            System.Collections.ObjectModel.ObservableCollection<System.Int32> myVisibleLayers = new System.Collections.ObjectModel.ObservableCollection<System.Int32>();
            myArcGISDynamicMapServiceLayer.VisibleLayers = myVisibleLayers;

            // Add the ArcGISDynamicMapServiceLayer to the Map.