Click or drag to resize
Code Example - LayerDrawingOptionCollection

Demonstrates using the existing rendering of a Dynamic Layer sub-layer of an ArcGISDynamicMapServiceLayer and switching to a custom rendering defined in code-behind for the Dynamic Layer.

Code Example
Layer Drawing Option Collection

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.

XAML
<Window x:Class="LayerDrawingOptionCollection.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
        Title="MainWindow" Height="600" Width="800">

    <Grid x:Name="LayoutRoot" Margin="0,0,0,-3">

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

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

                <!--Add a backdrop ArcGISTiledMapServiceLayer. -->
                <esri:ArcGISTiledMapServiceLayer ID="myArcGISTiledMapServiceLayer" 
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />

                <!-- Add an ArcGISDynamicMapServiceLayer to the Map. When the application loads make sure to turn off 
                    the visibility of all of the sub-layers (this will be done in code-behind). -->
                <esri:ArcGISDynamicMapServiceLayer ID="myArcGISDynamicMapServiceLayer" 
                   ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer" />

            </esri:Map>
        </esri:MapView>

        <!--    
        Add a Button to create a Dynamic Layer based upon the 'counties' sub-layer of the ArcGISDynamicMapServiceLayer. 
        Allow the user to choose which type of Rendering to apply to the Dynamic Layer.
        -->
        <RadioButton Content="Use existing Rendering defined by the published service" Height="16" Margin="10,58,0,0" 
          HorizontalAlignment="Left"  Name="RadioButton_ExistingRendering" VerticalAlignment="Top" Width="550" IsChecked="True"/>
        <RadioButton Content="Use a new Rendering defined on the client in the code-behind" Height="16" HorizontalAlignment="Left" 
          Margin="10,74,0,0" Name="RadioButton_NewRendering" VerticalAlignment="Top" Width="550" />
        <Button Content="Create a Dynamic Layer from the 'Counties' sub-layer of the ArcGISDynamicMapServiceLayer" Height="23" 
          HorizontalAlignment="Left" Margin="10,101,0,0" Name="Button1" VerticalAlignment="Top" Width="778" 
          Click="Button1_Click"/>

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

</Window>

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

namespace LayerDrawingOptionCollection
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.Text = "Click the button to add a Dynamic Layer based upon the 'Counties' sub-layer of the " + 
                "ArcGISDynamimcMapServiceLayer. Experiment with the choices for Rendering by trying each of the two radio buttons. " + 
                "One radio button uses the existing Rendering provided by ArcGIS Server service, the other does Rendering based " + 
                "upon the Client request.";

            // Get the ArcGISDynamicMapServiceLayer that was defined in XAML and ensure none of the sub-layers are drawn.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = null;
            myArcGISDynamicMapServiceLayer = (Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer)(MapView1.Map.Layers["myArcGISDynamicMapServiceLayer"]);
            myArcGISDynamicMapServiceLayer.VisibleLayers = new System.Collections.ObjectModel.ObservableCollection<int>();

            // Zoom to the Continental United States.
            Esri.ArcGISRuntime.Geometry.Envelope myExtent;
            myExtent = new Esri.ArcGISRuntime.Geometry.Envelope(-14070971, 2468307, -7345298, 6748281);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myExtent);
        }

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

            // Get the ArcGISDynamicMapServiceLayer that was defined in XAML but not shown visually in the Map.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = null;
            myArcGISDynamicMapServiceLayer = (Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer)(MapView1.Map.Layers["myArcGISDynamicMapServiceLayer"]);

            // Create a new DynamicLayerInfoCollection based upon the existing ArcGISDynamicMapServiceLayer.
            Esri.ArcGISRuntime.Layers.DynamicLayerInfoCollection myDynamicLayerInfoCollection = null;
            myDynamicLayerInfoCollection = myArcGISDynamicMapServiceLayer.CreateDynamicLayerInfosFromLayerInfos();

            // Loop through each DynamicLayerInfo object (i.e. sub-layer information about the ArcGISDynamicMapServiceLayer)
            foreach (Esri.ArcGISRuntime.Layers.DynamicLayerInfo myDynamicLayerInfo in myDynamicLayerInfoCollection)
            {

                // Get the ID of the sub-layer.
                int myID = myDynamicLayerInfo.ID;

                // Get the name of the sub-layer.
                string myName = myDynamicLayerInfo.Name;

                // Get the LayerSource object of the sub-layer. It will be a LayerMapSource Type because it is based on a 
                // published ArcGISDynamicMapServiceLayer.
                Esri.ArcGISRuntime.Layers.LayerSource myLayerSource = myDynamicLayerInfo.Source;
                if (myLayerSource is Esri.ArcGISRuntime.Layers.LayerMapSource)
                {

                    // We are only interested in the 'Counties' sub-layer for this sample code.
                    if (myName == "Counties")
                    {

                        // Create a new DynamicLayerInfo object (i.e. the container for the new Dynamic Layer)
                        Esri.ArcGISRuntime.Layers.DynamicLayerInfo newDynamicLayerInfo = new Esri.ArcGISRuntime.Layers.DynamicLayerInfo();

                        // Base the DynamicLayerInfo Properties on what was published in the 'Counties' sub-layer.
                        newDynamicLayerInfo.ID = myID;
                        newDynamicLayerInfo.Source = myLayerSource;

                        // Create a new DynamicLayerInfoCollection and add the DynamicLayerInfo object into it.
                        Esri.ArcGISRuntime.Layers.DynamicLayerInfoCollection newDynamicLayerInfoCollection = new Esri.ArcGISRuntime.Layers.DynamicLayerInfoCollection();
                        newDynamicLayerInfoCollection.Add(newDynamicLayerInfo);

                        // Create a new Dynamic Layer that is based upon the 'Counties' sub-layer of the ArcGISDynamicMapServiceLayer.
                        Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer newArcGISDynamicMapServiceLayer = new Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer();
                        newArcGISDynamicMapServiceLayer.ServiceUri = myArcGISDynamicMapServiceLayer.ServiceUri;
                        newArcGISDynamicMapServiceLayer.DynamicLayerInfos = newDynamicLayerInfoCollection;

                        // Allow the user to decide how they was the new Dynamic Layer Rendered.
                        if (RadioButton_ExistingRendering.IsChecked == true)
                        {

                            // Use the Rendering that is provided by default from the published ArcGIS Server ArcGISDynamicMapServiceLayer.
                            // There is no need to do anything as the Rendering is automatically transferred as part of use the existing
                            // LayerMapSource for the sub-layer.

                        }
                        else if (RadioButton_NewRendering.IsChecked == true)
                        {

                            // Use Rendering that is defined here on the client-side.

                            // Create a SimpleFillSymbol using the solid Yellow brush and set the Border to be Red with a thickness of 1.
                            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol newSimpleFillSymbol = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
                            newSimpleFillSymbol.Color = System.Windows.Media.Colors.Yellow;
                            newSimpleFillSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
                            Esri.ArcGISRuntime.Symbology.SimpleLineSymbol mySimpleLineSymbol = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
                            mySimpleLineSymbol.Color = System.Windows.Media.Colors.Red;
                            mySimpleLineSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid;
                            mySimpleLineSymbol.Width = 1;
                            newSimpleFillSymbol.Outline = mySimpleLineSymbol;

                            // Create a new SimpleRenderer based up the SimpleFillSymbol.
                            Esri.ArcGISRuntime.Symbology.SimpleRenderer newSimpleRenderer = new Esri.ArcGISRuntime.Symbology.SimpleRenderer();
                            newSimpleRenderer.Symbol = newSimpleFillSymbol;

                            // Create a new LayerDrawingOptions object which is key to applying our custom Rendering of the Dynamic Layer.
                            // It is imperative that the LayerDrawingOptions.LayerID = DynamicLayerInfo.ID so that the Dynamic Layer draws
                            // using the new symbology.
                            Esri.ArcGISRuntime.Layers.LayerDrawingOptions newLayerDrawingOptions = new Esri.ArcGISRuntime.Layers.LayerDrawingOptions();
                            newLayerDrawingOptions.LayerID = myID;
                            newLayerDrawingOptions.Renderer = newSimpleRenderer;

                            // Create a new LayerDrawinOptionsCollection and add the LayerDraingOptions object into it.
                            Esri.ArcGISRuntime.Layers.LayerDrawingOptionCollection newLayerDrawingOptionsCollection = new Esri.ArcGISRuntime.Layers.LayerDrawingOptionCollection();
                            newLayerDrawingOptionsCollection.Add(newLayerDrawingOptions);

                            // Apply the custom Rendering for the Dynamic Layer.
                            newArcGISDynamicMapServiceLayer.LayerDrawingOptions = newLayerDrawingOptionsCollection;

                        }

                        // Add the Dynamic Layer to the Map. This causes the round trip server request to occur.
                        MapView1.Map.Layers.Add(newArcGISDynamicMapServiceLayer);

                    }

                }

            }
        }

    }
}