Click or drag to resize
Code Example - LayerCollection

Demonstrates the work-flow of creating a layer dynamically via code-behind. The process of a layer: (1) initializing, (2) determining when initialization is complete, and (3) error trapping for bad initialization is described in detail.

Code Example
Layer 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="LayerCollection.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>

            <!-- ListBox to hold the ServiceUri strings that the user can choose. -->
            <ListBox x:Name="ListBox1" Height="75"/>

            <!-- Button to create an ArcGISDynaicMapServiceLayer at runtime based upon the ServiceUri chosen. -->
            <Button x:Name="Button1" Content="Add the Layer to the Map" Click="Button1_Click" />

            <!-- MapView with empty Map. -->
            <esri:MapView x:Name="MapView1" Height="500">
                <esri:Map x:Name="Map1">
                </esri:Map>
            </esri:MapView>

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

// This application shows a simplified work flow of creating a layer dynamically. MessageBox'es display:
// (1) the progress of the layer is initializing
// (2) when the layer has completely initialized
// (3) if there was a problem initializing displaying an error message

namespace LayerCollection
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Add various ServiceUri strings into the ListBox. Some are good and others do not exist.
            ListBox1.Items.Add("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"); // This is a good web service
            ListBox1.Items.Add("http://sampleserver1.arcgisonline.com/arcgis/rest/services/Specialty/BOGUS_SERVICE/MapServer"); // This web service does not exist
        }

        private async void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Create a variable to hold the name of the ServiceUri string.
            string theArcGISDynamicMapServiceLayerName = null;

            try
            {
                // Set the ServiceUri string to the user selected ListBox item.
                theArcGISDynamicMapServiceLayerName = ListBox1.SelectedItem.ToString();
            }
            catch (System.Exception)
            {
                // Notify the user that they must select a ListBox item.
                System.Windows.MessageBox.Show("You must select a ServiceUri string in the listbox before clicking the button.");
                return;
            }

            // Create a new ArcGISDynamicMapServiceLayer.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = new Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer();

            // Set the ServiceUri property.
            myArcGISDynamicMapServiceLayer.ServiceUri = theArcGISDynamicMapServiceLayerName;

            // Call the InitializeAsync method which will load the metadata for the layer. 
            System.Threading.Tasks.Task myTask = myArcGISDynamicMapServiceLayer.InitializeAsync();

            if (!myTask.IsCompleted)
            {
                // The Task (i.e. the layer loading) has not completed yet, let the user know that.
                System.Windows.MessageBox.Show("Initializing");

                try
                {
                    // Now wait for the Task to complete. This will lock the GUI until the Task completes. Notify the user of the result.  
                    await myTask;
                    System.Windows.MessageBox.Show("Initialized Successfully");

                    // Now add the ArcGISDynamicMapServiceLayer to the Map's LayerCollection
                    Esri.ArcGISRuntime.Layers.LayerCollection theLayerCollection = MapView1.Map.Layers;
                    theLayerCollection.Add(myArcGISDynamicMapServiceLayer);
                }
                catch (System.Exception ex)
                {
                    // The Task has completed but we have an error. Display to the user that there is something wrong with the layer.
                    Esri.ArcGISRuntime.Http.ArcGISWebException myArcGISWebException = (Esri.ArcGISRuntime.Http.ArcGISWebException)ex;
                    System.Windows.MessageBox.Show(ex.ToString(), "Initialized Failed");
                }
            }
        }

    }
}