Click or drag to resize
Code Example - ArcGISDynamicMapServiceLayer_GetDetails

Shows adding an ArcGISDynamicMapServiceLayer with a LayerDefinition applied (discovered by the ArcGISDynamicMapServiceLayer.GetDetailsAsync Method) and then modifying the LayerDefinition to change the features returned.

Code Example
ArcGISDynamic Map Service Layer Get Details

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="ArcGISDynamicMapServiceLayer_GetDetails.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,107,0,0" 
                      VerticalAlignment="Top" Height="453" Width="772">

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

                <!-- Add an ArcGISDynamicMapServiceLayer. -->
                <esri:ArcGISDynamicMapServiceLayer ID="MyUniqueName"
                    ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer" />

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

        <!-- Add a Button to apply to modify ArcGISDynamicMapServiceLayer.LayerDefinitions. -->
        <Button Content="Apply New LayerDefinition" Height="23" HorizontalAlignment="Left" Margin="7,79,0,0" 
          Name="Button_ApplyNewLayerDefinition" VerticalAlignment="Top" Width="184" 
          Click="Button_ApplyNewLayerDefinition_Click"/>

        <!-- TextBox to hold the LayerDefinitions text for the ArcGISDynamicMapServiceLayer. -->
        <TextBox Height="23" HorizontalAlignment="Left" Margin="206,79,0,0" Name="TextBox1" 
           VerticalAlignment="Top" Width="576" />

        <!-- TextBlock to hold the instructions on how to use the sample code. -->
        <TextBlock Height="74" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="775" 
                   TextWrapping="Wrap" Margin="7,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 "ArcGISDynamicMapServiceLayer_GetDetails.MainWindow" to be just "MainWindow".

namespace ArcGISDynamicMapServiceLayer_GetDetails
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            // This call is required by the designer.
            InitializeComponent();

            // Add any initialization after the InitializeComponent() call.

            // Add instructions on how to use the sample. 
            TextBlock1.Text = "When the application loads an ArcGISDynamicMapServiceLayer will display. One of the " + 
                "sub-layers has a LayerDefinition value specified which restrict how many features are returned in the " + 
                "Map. This server-side LayerDefintion will be displayed in the TextBox. Click " + 
                "your cursor in the TextBox for the sub-layer LayerDefinition and modify it. Then click the Button " + 
                "to see the changes. If you type a bogus LayerDefintion in the TextBox for the sub-layer, it will " + 
                "not display any features. Use the '1=1' to return all features for the sub-layer.";

            // Zoom to a specified target area in the in the Map.
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(13414332, 392041, 13415767, 392701);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myEnvelope);

            // Call the asynchronous function to display the DefinitionExpression of the ArcGISDynamicMapServiceLayer in a TextBox. 
            Init();
        }

        private async void Init()
        {
            // When the application loads get the LayerDefinition expression for a specific sub-layer.
            // It is necessary to use the ArcGISDynamicMapServiceLayer.GetDetails Method to obtain the LayerDefintion
            // for the sub-layer of the web service on ArcGIS Server via the FeatureServiceLayerInfo object. 
            // NOTE: Do not use the ArcGISDynamicMapServiceLayer.LayerDefinitions Property to try and obtain the
            // layer definition value as this the Property will return null/Nothing from the web service. 

            // Get the ArcGISDynamicMapServiceLayer from the Map by its ID value that was set in XAML.
            Esri.ArcGISRuntime.Layers.LayerCollection myLayerCollection = MapView1.Map.Layers;
            Esri.ArcGISRuntime.Layers.Layer myLayer = myLayerCollection["MyUniqueName"];
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer)myLayer;

            // Get the details for the first (i.e. 0) sub-layer. 
            Esri.ArcGISRuntime.ArcGISServices.FeatureServiceLayerInfo myFeatureServiceLayerInfo = await myArcGISDynamicMapServiceLayer.GetDetailsAsync(0);

            // Display the sub-layer's DefinitionExpression in the TextBox via the FeatureServiceLayerInfo object.
            TextBox1.Text = myFeatureServiceLayerInfo.DefinitionExpression;
        }

        private void Button_ApplyNewLayerDefinition_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // This function will apply the user changes to the sub-layer LayerDefintion to produce a new map
            // with different features returned.

            // Get the ArcGISDynamicMapServiceLayer from the Map Control by its ID value that was set in XAML.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer)(Map1.Layers["MyUniqueName"]);

            // Create an ObservableCollection of sub-layer LayerDefinitions. 
            System.Collections.ObjectModel.ObservableCollection<Esri.ArcGISRuntime.Layers.LayerDefinition> myObservableCollection = new System.Collections.ObjectModel.ObservableCollection<Esri.ArcGISRuntime.Layers.LayerDefinition>();

            // Create a new LayerDefinition object for the first (i.e. 0) sub-layer. Set the ID and Definition values.
            Esri.ArcGISRuntime.Layers.LayerDefinition myDefinition = new Esri.ArcGISRuntime.Layers.LayerDefinition();
            myDefinition.LayerID = 0;
            myDefinition.Definition = TextBox1.Text;
            myObservableCollection.Add(myDefinition);

            // Apply the custom LayerDefinition to the ArcGISDynamicMapServiceLayer; this will force the map to redraw.
            myArcGISDynamicMapServiceLayer.LayerDefinitions = myObservableCollection;
        }
    }
}