Click or drag to resize
Code Example - CreateDynamicLayerInfosFromLayerInfos

Demonstrates applying a custom rendering via XAML using the Dynamic Layer option of an ArcGISDynamicMapServiceLayer. Also, shows how to use the ArcGISDynamicMapServiceLayer.CreateDynamicLayerInfosFromLayerInfos Method to obtain the original rendering of the layer that was defined in the map service. The CreateDynamicLayerInfosFromLayerInfos is then used to reset the layer back to original rendering.

Code Example
Create Dynamic Layer Infos From Layer Infos

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

        <!-- Create a SimpleMarkerSymbol in the Resources for use in the custom Dynamic Layer. -->
            <esri:SimpleRenderer x:Key="myRenderer">
                    <esri:SimpleMarkerSymbol Color="Red" Style="Cross" Size="10"/>

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

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

                <!--Add a backdrop ArcGISTiledMapServiceLayer. -->
                <esri:ArcGISTiledMapServiceLayer ID="myArcGISTiledMapServiceLayer" 
                ServiceUri="" />

                Add a Dynamic Layer based on an existing ArcMap and ArcGIS Server map service ArcGISDynamicMapServiceLayer 
                sub-layer (specifically the 'cities' or [0] sub-layer). A Dynamic Layer is a web request where the client 
                application issues an ArcGIS Server command to generate an on-the-fly layer for display in the Map Control. 

                The following high-level things are occurring to create the Dynamic Layer: 
                (1) create a DynamicLayerInfo object and set the .ID and .Source Properties
                (2) add that DynamicLayerInfo object to the ArcGISDynamicMapServiceLayer .DynamicLayerInfos collection
                (3) create a LayerDrawingOptions object and set the .LayerID and .Renderer Properties
                (4) add that LayerDrawingOptions object to the ArcGISDynamicMapServiceLayer .LayerDrawingOptions collection
                <esri:ArcGISDynamicMapServiceLayer ID="myArcGISDynamicMapServiceLayer" 
                     ServiceUri="" >
                            <esri:DynamicLayerInfo ID="5">
                                    <esri:LayerMapSource MapLayerID="0"/>
                            <esri:LayerDrawingOptions LayerID="5" Renderer="{StaticResource myRenderer}"/>


        <!-- Add a Button and two Radio Buttons to change what is displayed in the Map. -->
        <RadioButton Content="Add all additional sub-layers except cities from the ArcGISDynamicMapServiceLayer" 
               Height="16" HorizontalAlignment="Left" Margin="12,137,0,0" VerticalAlignment="Top" 
               Name="RadioButton_AllSubLayersExceptCities" IsChecked="True"/>
        <RadioButton Content="Revert back to original ArcGISDynamicMapServiceLayer (i.e. all sub-layers)" Height="16" 
               HorizontalAlignment="Left" Margin="12,159,0,0" Name="RadioButton_AllSubLayers" VerticalAlignment="Top" />
        <Button Content="Modify the ArcGISDynamicMapServiceLayer" Height="23" 
          HorizontalAlignment="Left" Margin="0,181,0,0" Name="Button1" VerticalAlignment="Top" Width="792" 

        <!-- Add a TextBlock to hold the instructions on how to use the sample code. -->
        <TextBlock Height="137" 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 "CreateDynamicLayerInfosFromLayerInfos.MainWindow" to be just "MainWindow".

namespace CreateDynamicLayerInfosFromLayerInfos
    public partial class MainWindow : System.Windows.Window
        public MainWindow()

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.Text = "When the application code initially executes a Dynamic Layer is generated based upon an " + 
                "existing sub-layer of an ArcGISDynamicMapServiceLayer (specifically the 'cities' or [0] sub-layer) using custom " + 
                "Rendering. Select one of RadioButtons and click the Button to change the appearance of the ArcGISDynamicMapServiceLayer. " + 
                "You will need to re-run the application each time to test the different radio button options to see the difference. " + 
                "The default radio button (i.e. 'Add all additional sub-layers except cities from the ArcGISDynamicMapServiceLayer') " + 
                "will preserve the Rendering of the custom Dynamic Layer and add the default Rendering of the others sub-layers" + 
                "defined in the map service (except the cities layer). The other radio button (i.e. 'Revert back to original " + 
                "ArcGISDynamicMapServiceLayer() ') will override the Dynamic Layer and display the Rendering of the map service as " + 
                "defined by ArcMap and ArcGIS Server.";

            // Zoom to the southwest US.
            Esri.ArcGISRuntime.Geometry.Envelope myExtent;
            myExtent = new Esri.ArcGISRuntime.Geometry.Envelope(-13798791, 3563845, -9602538, 4595616);
            Map1.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myExtent);

        private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
            // NOTE: You will need to re-run the application each time to test the different radio button options to see the difference.

            // Get the ArcGISDynamicMapServiceLayer defined in XAML.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = null;
            myArcGISDynamicMapServiceLayer = (Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer)(MapView1.Map.Layers["myArcGISDynamicMapServiceLayer"]);

            // Get the DynamicLayerInfoCollection which contains information about all of the sub-layers of the 
            // ArcGISDynamicMapServiceLayer that was defined when the map service was initially created using
            // ArcMap and ArcGIS Server.
            Esri.ArcGISRuntime.Layers.DynamicLayerInfoCollection myDynamicLayerInfoCollection = myArcGISDynamicMapServiceLayer.CreateDynamicLayerInfosFromLayerInfos();

            if (RadioButton_AllSubLayers.IsChecked == true)
                // Set the original ArcGISDynamicMapServiceLayer .DynamicLayerInfoCollection that was defined by ArcMap and the 
                // ArcGIS Server map service to the current .DynamicLayerInfos. This will in effect override the Dynamic Layer 
                // and use the Rendering that was originally defined in the ArcMap and ArcGIS Server ArcGISDynamicmapServiceLayer 
                // map service.
                myArcGISDynamicMapServiceLayer.DynamicLayerInfos = myDynamicLayerInfoCollection;
            else if (RadioButton_AllSubLayersExceptCities.IsChecked == true)
                // This option will preserve the XAML defined Dynamic Layer and just add the originally defined ArcMap and ArcGIS 
                // Server map service sub-layers that are specified. Note that the 'cities' or [0] sub-layer will not be added 
                // using its default server side rendering because we want to preserve the custom symbology of the Dynamic Layer.