Click or drag to resize
Code Example - DynamicLayerInfos

Demonstrates using various Renderers on a Dynamic Layer (via XAML and code-behind) when the 'WorkspaceID' and 'DataSourceName' are known using a TableDataSource.

Code Example
Dynamic 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.

XAML
<Window x:Class="DynamicLayerInfos.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="10,179,0,0" 
              VerticalAlignment="Top" Height="381" 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 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 'File GDB' 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

                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 
                    ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer" >
                    <esri:ArcGISDynamicMapServiceLayer.DynamicLayerInfos>
                        <esri:DynamicLayerInfoCollection>
                            <esri:DynamicLayerInfo ID="101">
                                <esri:DynamicLayerInfo.Source>
                                    <esri:LayerDataSource>
                                        <esri:LayerDataSource.DataSource>
                                            <esri:TableDataSource WorkspaceID="MyDatabaseWorkspaceIDSSR2" 
                                                                  DataSourceName="ss6.gdb.highways"/>
                                        </esri:LayerDataSource.DataSource>
                                    </esri:LayerDataSource>
                                </esri:DynamicLayerInfo.Source>
                            </esri:DynamicLayerInfo>
                        </esri:DynamicLayerInfoCollection>
                    </esri:ArcGISDynamicMapServiceLayer.DynamicLayerInfos>
                    <esri:ArcGISDynamicMapServiceLayer.LayerDrawingOptions>
                        <esri:LayerDrawingOptionCollection>
                            <esri:LayerDrawingOptions LayerID="101" >
                                <esri:LayerDrawingOptions.Renderer>
                                    <esri:SimpleRenderer>
                                        <esri:SimpleLineSymbol Color="Black" Width="3" Style="Solid" />
                                    </esri:SimpleRenderer>
                                </esri:LayerDrawingOptions.Renderer>
                            </esri:LayerDrawingOptions>
                        </esri:LayerDrawingOptionCollection>
                    </esri:ArcGISDynamicMapServiceLayer.LayerDrawingOptions>
                </esri:ArcGISDynamicMapServiceLayer>

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

        <!-- Button to add a Dynamic Layer (using LayerDataSource) via code-behind. -->
        <Button Content="Add a Dynamic Layer using a LayerDataSource" Height="23" 
          HorizontalAlignment="Left" Margin="10,151,0,0" Name="Button1" VerticalAlignment="Top" Width="772" 
          Click="Button1_Click"/>

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

namespace DynamicLayerInfos
{
    /// <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 = "When the application starts a Dynamic Layer (Black lines) is added to the Map " + 
                "Control via XAML. Click the button to add another Dynamic Layer (Red solid circles) via code-behind. The " + 
                "Dynamic Layers used in this example code are based upon using a LayerDataSource that is undiscoverable via " + 
                "the public ArcGIS Server Directory REST pages. The 'WorkspaceID' and 'DataSourceName' for each Dynamic Layer " + 
                "must be provided to the developer by Manager/Administrator of the ArcGISDynamicMapServiceLayer. Examples of " + 
                "the 'WorkspaceID' and 'DataSourceName' are provided as comments in the XAML and 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(-14009741, 2050541, -3990987, 7665447);
            MapView1.Map.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: "CensusFileGDBWorkspaceID"
            // 
            // 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.cities";

            // 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 = 102; // 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 SimpleMarkerSymbol and sets it Style to a circle, Color to Red, and Size to 10 points.
            Esri.ArcGISRuntime.Symbology.SimpleMarkerSymbol mySimpleMarkerSymbol = new Esri.ArcGISRuntime.Symbology.SimpleMarkerSymbol();
            mySimpleMarkerSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleMarkerStyle.Circle;
            mySimpleMarkerSymbol.Color = System.Windows.Media.Colors.Red;
            mySimpleMarkerSymbol.Size = 10;

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

            // 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 = 102; // Must be the same as the DynammicLayerInfo.ID
            myLayerDrawingOptions.Renderer = mySimpleRenderer;

            // Create a new LayerDrawingOptionsCollection and add the LayerDrawingOptions 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;

            // Apply a LayerDefinition where clause to restrict the amount of features that are returned. 
            Esri.ArcGISRuntime.Layers.LayerDefinition myLayerDefinition = new Esri.ArcGISRuntime.Layers.LayerDefinition();
            myLayerDefinition.Definition = "POP2000 > 100000";
            myLayerDefinition.LayerID = 102;
            myArcGISDynamicMapServiceLayer.LayerDefinitions.Add(myLayerDefinition);

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

        }

    }
}