Click or drag to resize
Code Example - DynamicLayerInfoCollection

Demonstrates using various Renderers on a Dynamic Layer when the 'WorkspaceID' and 'DataSourceName' are known using a TableDataSource.

Code Example
Dynamic Layer Info 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="DynamicLayerInfoCollection.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,110,0,0"
                      VerticalAlignment="Top" Height="444" Width="782">

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

                <!-- Add a backdrop ArcGISTiledMapServiceLayer. The SpatialReference is 4326 (lat/long). -->
                <esri:ArcGISTiledMapServiceLayer ID="myArcGISTiledMapServiceLayer" 
                ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />

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

        <!-- 
        Button to add a Dynamic Layer (using a TableDataSource Type of LayerDataSource) via code-behind. 
        Users choose a different RadioButton to see the Rendering effect on the Dynamic Layer.
        -->
        <RadioButton Content="SimpleRenderer" Height="16" HorizontalAlignment="Left" Margin="12,60,0,0" 
               Name="RadioButton_SimpleRenderer" VerticalAlignment="Top" IsChecked="True"/>
        <RadioButton Content="ClassBreaksRenderer" Height="16" HorizontalAlignment="Left" Margin="160,60,0,0" 
               Name="RadioButton_ClassBreaksRenderer" VerticalAlignment="Top" />
        <RadioButton Content="UniqueValueRenderer" Height="16" HorizontalAlignment="Left" Margin="330,60,0,0" 
               Name="RadioButton_UniqueValueRenderer" VerticalAlignment="Top" />
        <Button Content="Add a LayerDataSource based Dynamic Layer using the specified Renderer" Height="24" 
          HorizontalAlignment="Left" Margin="0,81,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="55" 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 "DynamicLayerInfoCollection.MainWindow" to be just "MainWindow".

namespace DynamicLayerInfoCollection
{
    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 = "Choose a desired Renderer and click the Button to create a Dynamic Layer on-the-fly " + 
                "using code-behind. You can repeat this process for each Renderer to see the different effect. Examples of the " + 
                "'WorkspaceID' and 'DataSourceName' are provided as comments in the code-behind for a public ArcGIS Server that " + 
                "has Dynamic Layers enabled via the LayerDataSource object.";

            // Zoom to the continental US.
            Esri.ArcGISRuntime.Geometry.Envelope myExtent;
            myExtent = new Esri.ArcGISRuntime.Geometry.Envelope(-133.12, 13.68, -26.63, 60.93);
            Map1.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myExtent);
        }

        private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Add a Layer on-the-fly based on an ArcGISDynamicMapServiceLayer that has Dynamic Layer capabilities enabled.
            // The Dynamic Layer is being generated uses the 'Workspace Type' of 'Database' via the LayerDataSource object. Review the
            // ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer Class documentation in the API reference for a full discussion.
            // In order to create this Dynamic Layer, information was shared between the Manager/Administrator of the
            // ArcGISDynamicMapServiceLayer ArcGIS Server service and the application developer.
            // 
            // The Url for the ArcGISDynamicMapServiceLayer is: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer".
            // 
            // The WorkspaceID used in the LayerDataSource for the Dynamic Layer is: "MyDatabaseWorkspaceIDSSR2"
            // 
            // There are several DataSourceName types associated with the WorkspaceID that could be used to create the Rendering 
            // of a Dynamic Layer. Think of the DataSourceName as the string name of the layer that will be drawn dynamically.
            // A listing of the available DataSourceNames (and their geometry type) are:
            // "ss6.gdb.highways" <== Polylines
            // "ss6.gdb.states" <== Polygons
            // "ss6.gdb.counties" <== Polygons
            // "ss6.gdb.cities" <== Points
            // "ss6.gdb.lakes" <== Polygons
            // "ss6.gdb.places" <== Points
            // "ss6.gdb.rivers" <== Polylines
            // "ss6.gdb.urbanareas" <== Polygons
            // "ss6.gdb.railroads" <== Polylines

            // Create a new TableDataSource object and set it's .WorkspaceID and .DataSourceName Properties to valid values.
            Esri.ArcGISRuntime.Layers.TableDataSource myTableDataSource = new Esri.ArcGISRuntime.Layers.TableDataSource();
            myTableDataSource.WorkspaceID = "MyDatabaseWorkspaceIDSSR2";
            myTableDataSource.DataSourceName = "ss6.gdb.states";

            // Create a new LayerDataSource and set its DataSource Property to the TableDataSource.
            Esri.ArcGISRuntime.Layers.LayerDataSource myLayerDataSource = new Esri.ArcGISRuntime.Layers.LayerDataSource();
            myLayerDataSource.DataSource = myTableDataSource;

            // Create a new DynamicLayerInfo object and set its ID and Source Properties.
            Esri.ArcGISRuntime.Layers.DynamicLayerInfo myDynamicLayerInfo = new Esri.ArcGISRuntime.Layers.DynamicLayerInfo();
            myDynamicLayerInfo.ID = 99; // Must be the same as the LayerDrawingOptions.LayerID
            myDynamicLayerInfo.Source = myLayerDataSource;

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

            // 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 myLayerDrawingOptions = new Esri.ArcGISRuntime.Layers.LayerDrawingOptions();
            myLayerDrawingOptions.LayerID = 99; // Must be the same as the DynammicLayerInfo.ID

            // Set the Renderer of the Dynamic Layer based upon the user choice from the RadioButtons in the GUI.
            if (RadioButton_SimpleRenderer.IsChecked == true)
            {
                // Use a SimpleRenderer.
                myLayerDrawingOptions.Renderer = MakeSimpleRendererFill();
            }
            else if (RadioButton_ClassBreaksRenderer.IsChecked == true)
            {
                // Use a ClassBreaksRenderer. Specify the string name of the Field that Rendering will occur on.
                myLayerDrawingOptions.Renderer = MakeClassBreaksRenderer_Fill("pop2000");
            }
            else if (RadioButton_UniqueValueRenderer.IsChecked == true)
            {
                // Use a UniqueValueRenderer. Specify the string name of the Field that Rendering will occur on.
                myLayerDrawingOptions.Renderer = MakeUniqueValueRenderer_Fill("sub_region");
            }

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

            // Create a new Dynamic Layer that is based upon on a LayerDataSource of an ArcGISDynamicMapServiceLayer
            // and apply it's custom rendering.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = new Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer();
            myArcGISDynamicMapServiceLayer.ServiceUri = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer";
            myArcGISDynamicMapServiceLayer.DynamicLayerInfos = myDynamicLayerInfoCollection;
            myArcGISDynamicMapServiceLayer.LayerDrawingOptions = myLayerDrawingOptionsCollection;

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

        public Esri.ArcGISRuntime.Symbology.SimpleRenderer MakeSimpleRendererFill()
        {
            // Create a SimpleFillSymbol that is a red fill with a black outline.
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol.Color = System.Windows.Media.Colors.Red;
            Esri.ArcGISRuntime.Symbology.SimpleLineSymbol mySimpleLineSymbol = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
            mySimpleLineSymbol.Color = System.Windows.Media.Colors.Black;
            mySimpleLineSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid;
            mySimpleLineSymbol.Width = 1;
            mySimpleFillSymbol.Outline = mySimpleLineSymbol;

            // Create a new SimpleRenderer based upon the SimpleFillSymbol.
            Esri.ArcGISRuntime.Symbology.SimpleRenderer mySimpleRenderer = new Esri.ArcGISRuntime.Symbology.SimpleRenderer();
            mySimpleRenderer.Symbol = mySimpleFillSymbol;

            return mySimpleRenderer;
        }

        public Esri.ArcGISRuntime.Symbology.ClassBreaksRenderer MakeClassBreaksRenderer_Fill(string theFieldName)
        {
            // Create a ClassBreaksRenderer using what is specified for theFieldName as the Field to 
            // perform the Rendering. In our case the various ClassBreakInfo objects have hard-coded 
            // .MinimumValue and .MaximumValue settings that are appropriate for the .Field. If you
            // use a different DataSourceName you will need to adjust these values accordingly.
            Esri.ArcGISRuntime.Symbology.ClassBreaksRenderer myClassBreaksRenderer = new Esri.ArcGISRuntime.Symbology.ClassBreaksRenderer();
            myClassBreaksRenderer.Field = theFieldName; // Requires a numeric Field

            // Define the outline Symbol for the Fills
            Esri.ArcGISRuntime.Symbology.SimpleLineSymbol myBlackSolidOutline = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
            myBlackSolidOutline.Color = System.Windows.Media.Colors.Black;
            myBlackSolidOutline.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid;
            myBlackSolidOutline.Width = 1;

            // Group #1
            Esri.ArcGISRuntime.Symbology.ClassBreakInfo myClassBreakInfo1 = new Esri.ArcGISRuntime.Symbology.ClassBreakInfo();
            myClassBreakInfo1.Minimum = 0;
            myClassBreakInfo1.Maximum = 1000000;
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol1 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol1.Color = System.Windows.Media.Colors.Yellow;
            mySimpleFillSymbol1.Outline = myBlackSolidOutline;
            mySimpleFillSymbol1.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myClassBreakInfo1.Symbol = mySimpleFillSymbol1;
            myClassBreaksRenderer.Infos.Add(myClassBreakInfo1);

            // Group #2
            Esri.ArcGISRuntime.Symbology.ClassBreakInfo myClassBreakInfo2 = new Esri.ArcGISRuntime.Symbology.ClassBreakInfo();
            myClassBreakInfo2.Minimum = 1000001;
            myClassBreakInfo2.Maximum = 10000000;
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol2 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol2.Color = System.Windows.Media.Colors.Green;
            mySimpleFillSymbol2.Outline = myBlackSolidOutline;
            mySimpleFillSymbol2.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myClassBreakInfo2.Symbol = mySimpleFillSymbol2;
            myClassBreaksRenderer.Infos.Add(myClassBreakInfo2);

            // Group #3
            Esri.ArcGISRuntime.Symbology.ClassBreakInfo myClassBreakInfo3 = new Esri.ArcGISRuntime.Symbology.ClassBreakInfo();
            myClassBreakInfo3.Minimum = 10000001;
            myClassBreakInfo3.Maximum = 100000000;
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol3 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol3.Color = System.Windows.Media.Colors.Red;
            mySimpleFillSymbol3.Outline = myBlackSolidOutline;
            mySimpleFillSymbol3.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myClassBreakInfo3.Symbol = mySimpleFillSymbol3;
            myClassBreaksRenderer.Infos.Add(myClassBreakInfo3);

            return myClassBreaksRenderer;
        }

        public Esri.ArcGISRuntime.Symbology.UniqueValueRenderer MakeUniqueValueRenderer_Fill(string theFieldName)
        {
            // Create a UniqueValueRenderer using what is specified for theFieldName as the Field to 
            // perform the Rendering. In our case the various UniqueValueInfo objects have hard-coded 
            // .Value settings that are appropriate for the .Field. If you use a different 
            // DataSourceName you will need to adjust these values accordingly.
            Esri.ArcGISRuntime.Symbology.UniqueValueRenderer myUniqueValueRenderer = new Esri.ArcGISRuntime.Symbology.UniqueValueRenderer();
            myUniqueValueRenderer.Fields.Add(theFieldName);

            // Define the outline Symbol for the Fills
            Esri.ArcGISRuntime.Symbology.SimpleLineSymbol myBlackSolidOutline = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
            myBlackSolidOutline.Color = System.Windows.Media.Colors.Black;
            myBlackSolidOutline.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid;
            myBlackSolidOutline.Width = 1;

            // Group #1
            Esri.ArcGISRuntime.Symbology.UniqueValueInfo myUniqueValueInfo1 = new Esri.ArcGISRuntime.Symbology.UniqueValueInfo();
            myUniqueValueInfo1.Values.Add("Pacific");
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol1 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol1.Color = System.Windows.Media.Colors.Yellow;
            mySimpleFillSymbol1.Outline = myBlackSolidOutline;
            mySimpleFillSymbol1.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myUniqueValueInfo1.Symbol = mySimpleFillSymbol1;
            myUniqueValueRenderer.Infos.Add(myUniqueValueInfo1);

            // Group #2
            Esri.ArcGISRuntime.Symbology.UniqueValueInfo myUniqueValueInfo2 = new Esri.ArcGISRuntime.Symbology.UniqueValueInfo();
            myUniqueValueInfo2.Values.Add("W N Cen");
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol2 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol2.Color = System.Windows.Media.Colors.Blue;
            mySimpleFillSymbol2.Outline = myBlackSolidOutline;
            mySimpleFillSymbol2.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myUniqueValueInfo2.Symbol = mySimpleFillSymbol2;
            myUniqueValueRenderer.Infos.Add(myUniqueValueInfo2);

            // Group #3
            Esri.ArcGISRuntime.Symbology.UniqueValueInfo myUniqueValueInfo3 = new Esri.ArcGISRuntime.Symbology.UniqueValueInfo();
            myUniqueValueInfo3.Values.Add("W S Cen");
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol3 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol3.Color = System.Windows.Media.Colors.Red;
            mySimpleFillSymbol3.Outline = myBlackSolidOutline;
            mySimpleFillSymbol3.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myUniqueValueInfo3.Symbol = mySimpleFillSymbol3;
            myUniqueValueRenderer.Infos.Add(myUniqueValueInfo3);

            // Group #4
            Esri.ArcGISRuntime.Symbology.UniqueValueInfo myUniqueValueInfo4 = new Esri.ArcGISRuntime.Symbology.UniqueValueInfo();
            myUniqueValueInfo4.Values.Add("E N Cen");
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol4 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol4.Color = System.Windows.Media.Colors.White;
            mySimpleFillSymbol4.Outline = myBlackSolidOutline;
            mySimpleFillSymbol4.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myUniqueValueInfo4.Symbol = mySimpleFillSymbol4;
            myUniqueValueRenderer.Infos.Add(myUniqueValueInfo4);

            // Group #5
            Esri.ArcGISRuntime.Symbology.UniqueValueInfo myUniqueValueInfo5 = new Esri.ArcGISRuntime.Symbology.UniqueValueInfo();
            myUniqueValueInfo5.Values.Add("Mtn");
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol5 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol5.Color = System.Windows.Media.Colors.Brown;
            mySimpleFillSymbol5.Outline = myBlackSolidOutline;
            mySimpleFillSymbol5.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myUniqueValueInfo5.Symbol = mySimpleFillSymbol5;
            myUniqueValueRenderer.Infos.Add(myUniqueValueInfo5);

            // Group #6
            Esri.ArcGISRuntime.Symbology.UniqueValueInfo myUniqueValueInfo6 = new Esri.ArcGISRuntime.Symbology.UniqueValueInfo();
            myUniqueValueInfo6.Values.Add("N Eng");
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol6 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol6.Color = System.Windows.Media.Colors.Cyan;
            mySimpleFillSymbol6.Outline = myBlackSolidOutline;
            mySimpleFillSymbol6.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myUniqueValueInfo6.Symbol = mySimpleFillSymbol6;
            myUniqueValueRenderer.Infos.Add(myUniqueValueInfo6);

            // Group #7
            Esri.ArcGISRuntime.Symbology.UniqueValueInfo myUniqueValueInfo7 = new Esri.ArcGISRuntime.Symbology.UniqueValueInfo();
            myUniqueValueInfo7.Values.Add("E S Cen");
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol7 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol7.Color = System.Windows.Media.Colors.Green;
            mySimpleFillSymbol7.Outline = myBlackSolidOutline;
            mySimpleFillSymbol7.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myUniqueValueInfo7.Symbol = mySimpleFillSymbol7;
            myUniqueValueRenderer.Infos.Add(myUniqueValueInfo7);

            // Group #8
            Esri.ArcGISRuntime.Symbology.UniqueValueInfo myUniqueValueInfo8 = new Esri.ArcGISRuntime.Symbology.UniqueValueInfo();
            myUniqueValueInfo8.Values.Add("Mid Atl");
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol8 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol8.Color = System.Windows.Media.Colors.Gray;
            mySimpleFillSymbol8.Outline = myBlackSolidOutline;
            mySimpleFillSymbol8.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myUniqueValueInfo8.Symbol = mySimpleFillSymbol8;
            myUniqueValueRenderer.Infos.Add(myUniqueValueInfo8);

            // Group #9
            Esri.ArcGISRuntime.Symbology.UniqueValueInfo myUniqueValueInfo9 = new Esri.ArcGISRuntime.Symbology.UniqueValueInfo();
            myUniqueValueInfo9.Values.Add("S Atl");
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol9 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol9.Color = System.Windows.Media.Colors.Orange;
            mySimpleFillSymbol9.Outline = myBlackSolidOutline;
            mySimpleFillSymbol9.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myUniqueValueInfo9.Symbol = mySimpleFillSymbol9;
            myUniqueValueRenderer.Infos.Add(myUniqueValueInfo9);

            return myUniqueValueRenderer;
        }

    }
}