Click or drag to resize
Code Example - LayerDrawingOptions

Demonstrates changing the rendering of the first sub-layer of an ArcGISDynamicMapServiceLayer using the Dynamic Layer capabilities of ArcGIS Server 10.1 and higher.

Code Example
Layer Drawing Options

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="LayerDrawingOptions.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">

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

            <!-- 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. -->
                <esri:ArcGISDynamicMapServiceLayer ID="myArcGISDynamicMapServiceLayer"
                    ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer" />

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


        <!-- Add a Button to change the Rendering of the first sub-layer of the ArcGISDynamicMapServiceLayer. -->
        <Button Content="Change the Rendering of the first sub-layer in the ArcGISDynamicMapServiceLayer" Height="23" 
          HorizontalAlignment="Left" Margin="0,80,0,0" Name="Button1" VerticalAlignment="Top" Width="782" 
          Click="Button1_Click"/>

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

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

using System.Linq;

namespace LayerDrawingOptions
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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 change the Rendering of the first sub-layer of the ArcGISDynamimcMapServiceLayer " + 
                "using the Dynamic Layer capabilities of ArcGIS Server 10.1 and higher.";

            // Zoom to the South-Western United States.
            Esri.ArcGISRuntime.Geometry.Envelope myExtent;
            myExtent = new Esri.ArcGISRuntime.Geometry.Envelope(-13468458, 3546553, -12238506, 4578324);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myExtent);
        }

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

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

            // Using Dynamic Layers to change the symbology of an ArcGISDynamicMapServiceLayer is only available on ArcGIS Server 10.1 and 
            // higher. Ensure that we have correct service before proceeding.
            await myArcGISDynamicMapServiceLayer.InitializeAsync();
            Esri.ArcGISRuntime.ArcGISServices.MapServiceInfo myMapServiceInfo = myArcGISDynamicMapServiceLayer.ServiceInfo;
            double version = myMapServiceInfo.CurrentVersion;
            if (version >= 10.1)
            {

                // Get the DynamicLayerInfoCollection which contains information about the sub-layers of an ArcGISDynamicMapServiceLayer
                Esri.ArcGISRuntime.Layers.DynamicLayerInfoCollection myDynamicLayerInfoCollection = null;
                myDynamicLayerInfoCollection = myArcGISDynamicMapServiceLayer.CreateDynamicLayerInfosFromLayerInfos();

                // Get the first sub-layer information (i.e. the 'Cities' or [0] sub-layer)
                Esri.ArcGISRuntime.Layers.DynamicLayerInfo myDynamicLayerInfo = myDynamicLayerInfoCollection.FirstOrDefault();

                // Define a new SimpleMarkerSymbol (a green cross, size 10)
                Esri.ArcGISRuntime.Symbology.SimpleMarkerSymbol mySimpleMarkerSymbol = null;
                mySimpleMarkerSymbol = new Esri.ArcGISRuntime.Symbology.SimpleMarkerSymbol();
                mySimpleMarkerSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleMarkerStyle.Cross;
                mySimpleMarkerSymbol.Color = System.Windows.Media.Colors.Green;
                mySimpleMarkerSymbol.Size = 10;

                // Define a new SimpleRenderer and set the Symbol to the new SymbolMarkerSymbol just created.
                Esri.ArcGISRuntime.Symbology.SimpleRenderer mySimpleRenderer = null;
                mySimpleRenderer = new Esri.ArcGISRuntime.Symbology.SimpleRenderer();
                mySimpleRenderer.Symbol = mySimpleMarkerSymbol;

                // Create a new LayerDrawingOptions object which will change the symbology of the existing first sub-layer of 
                // the ArcGISDynamicMapServiceLayer.
                Esri.ArcGISRuntime.Layers.LayerDrawingOptions myLayerDrawinOptions = null;
                myLayerDrawinOptions = new Esri.ArcGISRuntime.Layers.LayerDrawingOptions();
                myLayerDrawinOptions.LayerID = myDynamicLayerInfo.ID;
                myLayerDrawinOptions.Renderer = mySimpleRenderer;

                // Create a LayerDrawingOptionsCollection to house the individual LayerDrawingOptions objects.
                Esri.ArcGISRuntime.Layers.LayerDrawingOptionCollection myLayerDrawingOptionsCollection = null;
                myLayerDrawingOptionsCollection = new Esri.ArcGISRuntime.Layers.LayerDrawingOptionCollection();

                // Apply the new settings of the Dynamic Layer and refresh the ArcGISDynamicMapServiceLayer. 
                myArcGISDynamicMapServiceLayer.LayerDrawingOptions = myLayerDrawingOptionsCollection;
                myArcGISDynamicMapServiceLayer.LayerDrawingOptions.Add(myLayerDrawinOptions);

            }
            else
            {

                // Notify the user if using a Dynamic Layer is not possible.
                System.Windows.MessageBox.Show("Changing the symbology of a Dynamic Layer not possible. Upgrade to ArcGIS Server 10.1 or higher.");

            }

        }



    }
}