Click or drag to resize
Code Example - Mapview_LayerLoaded

Demonstrates how to determine if a particular layer has loaded via the MapView.LayerLoaded Event.

Code Example
Map View Layer Loaded

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="MapView_LayerLoaded.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">

            <!-- Add a default MapView. Define an EventHandler for the MapView.LayerLoaded event. -->
            <esri:MapView x:Name="MapView1" LayerLoaded="MapView1_LayerLoaded" Height="464" >

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

                    <!-- Add a valid FeatureLayer. -->
                    <esri:FeatureLayer ID="States">
                        <esri:FeatureLayer.FeatureTable>
                            <esri:ServiceFeatureTable ServiceUri="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/2"/>
                        </esri:FeatureLayer.FeatureTable>
                    </esri:FeatureLayer>

                    <!-- Add a valid FeatureLayer. -->
                    <esri:FeatureLayer ID="Rivers">
                        <esri:FeatureLayer.FeatureTable>
                            <esri:ServiceFeatureTable ServiceUri="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/1"/>
                        </esri:FeatureLayer.FeatureTable>
                    </esri:FeatureLayer>

                    <!-- Add an invalid FeatureLayer. This layer will not load. -->
                    <esri:FeatureLayer ID="BOGUS">
                        <esri:FeatureLayer.FeatureTable>
                            <esri:ServiceFeatureTable ServiceUri="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/9"/>
                        </esri:FeatureLayer.FeatureTable>
                    </esri:FeatureLayer>

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

            <!-- Add a TextBox to display the status of the layers loading in the Map. -->
            <TextBox x:Name="TextBox1" Width="800" Height="102" FontSize="16" TextWrapping="Wrap" Margin="0,0,-8,0"/>

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

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

            // Zoom to the continental US.
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(-126.56, 15.97, -66.40, 59.02);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myEnvelope);
        }

        private void MapView1_LayerLoaded(object sender, Esri.ArcGISRuntime.Controls.LayerLoadedEventArgs e)
        {
            // This event fires each time a layer loads in the Map Control.

            if (e.LoadError == null)
            {

                // The layer loaded successfully. Display to the user the Layer ID and it's successful loading.  
                TextBox1.Text = TextBox1.Text + string.Format("Layer : {0} loaded successfully!" + System.Environment.NewLine, e.Layer.ID);

                // Exit the function.
                return;

            }

            // The layer did not load successfully. Display to the user the Layer ID and a message about the unsuccessful loading.
            TextBox1.Text = TextBox1.Text + string.Format("Layer : {0} loaded UNSUCCESSFULY! - {1}", e.Layer.ID, e.LoadError.Message);
        }
    }
}