Click or drag to resize
Code Example - ArcGISDynamicMapServiceLayerXamlAndCodeBehind

Shows creating an ArcGISDynamicMapServiceLayer in XAML and via code-behind. The ArcGISDynamicMapServiceLayer.InitializeAsync Method is used to obtain metadata about the layer (i.e. ID and Copyright) using the Async/Task framework.

Code Example
ArcGISDynamic Map Service Layer Xaml And Code Behind

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="ArcGISDynamicMapServiceLayerXamlAndCodeBehind.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="600" Width="800"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">

    <Grid>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Vertical">

                <!-- TextBlock to provide the instructions on how to use the sample code. It will be 
                populated with instructions in the code-behind when the application loads. -->
                <TextBlock Height="174" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" 
                           Width="770" TextWrapping="Wrap"  Margin="10,2,0,0" />

            </StackPanel>
            <StackPanel Orientation="Vertical">

                <!-- Button to add an ArcGISDynamicMapServiceLayer via code-behind. -->
                <Button Content="Add another ArcGISDynamicMapServiceLayer (via code-behind) for the specified Url." 
                        x:Name="Button1" Click="Button1_Click" Width="780" HorizontalAlignment="Left"/>

            </StackPanel>
            <StackPanel Orientation="Horizontal">

                <!-- A MapView Control to display various GIS layers. -->
                <esri:MapView x:Name="MapView1" Width="400" Height="300" VerticalAlignment="Top" Margin="2,2,2,2">

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

                        <!-- Add an ArcGISDynamicMapServiceLayer via Xaml. Set the ID and ImageFormat properties. -->
                        <esri:ArcGISDynamicMapServiceLayer ID="World_Light_Gray_Base" ImageFormat="PNG24" 
                          ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer"/>
                    </esri:Map>
                </esri:MapView>

                <StackPanel Orientation="Vertical">

                    <!-- TextBox to display information about the various layers in the Map. -->
                    <TextBox x:Name="TextBox1" TextWrapping="Wrap" Height="300" Width="370" Margin="2,2,2,2"/>

                </StackPanel>

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

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

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

            // Provide instructions on how to use the sample. 
            TextBlock1.Text = "When the application loads an ArcGISDynamicMapServiceLayer will " + 
                "automatically be added to the Map (it was specified in XAML). Click the Button to add another" + 
                "ArcGISDynaicMapServiceLayer to the Map (it will be added via code-behind). The ID  and copyright " + 
                "information of each layer will displayed in the TextBox.";

            // Create a Geometry based on an Envelope set to a particular extent of North and Central America using a WebMercator Spatial Reference.
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = new Esri.ArcGISRuntime.Geometry.Envelope(-20686341, 712444, -5228835, 12734949, Esri.ArcGISRuntime.Geometry.SpatialReferences.WebMercator);

            // Set the initial extent of the Map using the InitialViewpoint Property to that of our Geometry.
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myGeometry);
            // Call our custom 'async' function to display information about the ArcGISDynamicMapSericeLayer that was added in XAML.
            Init();
        }

        public async void Init()
        {
            // This function will display information about the ArcGISDynamicMapServiceLayer that was added in XAML.

            // Get the ArcGISDynamicMapServiceLayer by it's index number.        
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = 
                (Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer) MapView1.Map.Layers[0];

            // Initialize the ArcGISDynamicMapServiceLayer which allows for obtain various Property information.
            await myArcGISDynamicMapServiceLayer.InitializeAsync();

            // Get the ID of the ArcGISDynamicMapServiceLayer.
            string myID = myArcGISDynamicMapServiceLayer.ID;

            // Create a StringBuilder object to hold information about the ArcGISDynamicMapServiceLayer.
            System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();

            // Add the ID of the ArcGISDynamicMapServiceLayer to the StringBuilder.
            myStringBuilder.Append("The 1st ArcGISDynamicMapServiceLayer was added via XAML and it's ID is: " + myID + System.Environment.NewLine);

            // Get the MapServiceInfo details about the ArcGISDynamicMapServiceLayer.
            Esri.ArcGISRuntime.ArcGISServices.MapServiceInfo myMapServiceInfo = myArcGISDynamicMapServiceLayer.ServiceInfo;

            // Get the copyright information about the ArcGISDynamicMapServiceLayer and add to the StringBuilder.
            string myCopyright = myMapServiceInfo.CopyrightText;
            myStringBuilder.Append(myCopyright + System.Environment.NewLine);

            // Display the results of the StringBuilder text to the user.
            TextBox1.Text = myStringBuilder.ToString();
        }

        private async void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // This function will add an ArcGISDynamicMapServiceLayer to the Map via code behind.

            // Create a new Uri that point to an ArcGISDynamicMapServiceLayer.
            System.Uri myUri = new System.Uri("http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Unemployment_Rate/MapServer");

            // Create a new instance of an ArcGISDynamicMapServiceLayer.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = null;
            myArcGISDynamicMapServiceLayer = new Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer();

            // Set the Uri and ID of the ArcGISDynamicMapSericveLayer.
            myArcGISDynamicMapServiceLayer.ServiceUri = myUri.ToString();
            myArcGISDynamicMapServiceLayer.ID = "MyArcGISDynamicMapServiceLayer";

            // For better image quality set the ImageFormat to be PNG24 (the Default is PNG8)
            myArcGISDynamicMapServiceLayer.ImageFormat = Esri.ArcGISRuntime.ArcGISServices.ImageFormat.PNG24;

            // Add the ArcGISDynamicMapServiceLayer to the Map.
            MapView1.Map.Layers.Add(myArcGISDynamicMapServiceLayer);

            // Initialize the ArcGISDynamicMapServiceLayer which allows for obtain various Property information.
            await myArcGISDynamicMapServiceLayer.InitializeAsync();

            // Get the ID of the ArcGISDynamicMapServiceLayer.
            string myID = myArcGISDynamicMapServiceLayer.ID;

            // Create a StringBuilder object to hold information about the ArcGISDynamicMapServiceLayer.
            System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();
            myStringBuilder.Append(TextBox1.Text + System.Environment.NewLine);
            myStringBuilder.Append(System.Environment.NewLine);

            // Add the ID of the ArcGISDynamicMapServiceLayer to the StringBuilder.
            myStringBuilder.Append("The 2nd ArcGISDynamicMapServiceLayer was added via code-behind and it's ID is: " + 
                myID + System.Environment.NewLine);

            // Get the MapServiceInfo details about the ArcGISDynamicMapServiceLayer.
            Esri.ArcGISRuntime.ArcGISServices.MapServiceInfo myMapServiceInfo = myArcGISDynamicMapServiceLayer.ServiceInfo;

            // Get the copyright information about the ArcGISDynamicMapServiceLayer and add to the StringBuilder.
            string myCopyright = myMapServiceInfo.CopyrightText;
            myStringBuilder.Append(myCopyright + System.Environment.NewLine);

            // Display the results of the StringBuilder text to the user.
            TextBox1.Text = myStringBuilder.ToString();
        }
    }
}