Click or drag to resize
Code Example - LayersInitialized

Demonstrates implementing logic which is dependent on the layer collection being initialized.

Code Example
Layers Initialized

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.

<Window x:Class="LayersInitialized.MainWindow"
        Height="600" Width="800">


        <!-- Add a MapView Control. -->
        <esri:MapView x:Name="MapView1">

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

                <!-- Add an ArcGISTiledMapServiceLayer. -->
                <esri:ArcGISTiledMapServiceLayer ID="WorldStreetMap" 
                      ServiceUri="" />

        <Border HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10" Padding="5" Background="White" 
                BorderBrush="Black" BorderThickness="1">
                <DropShadowEffect />
            <StackPanel Orientation="Vertical">

                <!-- Set to the Binding to the TextBox.Text Property. This way when things change in the code-behind 
                it will be displayed in the GUI. -->
                <TextBlock Width="150" Margin="5" Text="{Binding LayersInitializedProperty}" 
                           Background="Black" Foreground="White"/>


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

using System.Linq;

// NOTE: This sample demonstrates implementing logic which is dependent on the layer collection being initialized. An
// MVVM style of programming is used.

namespace LayersInitialized
    public partial class MainWindow : System.Windows.Window, System.ComponentModel.INotifyPropertyChanged

        // Inherit the functionality from the basic Window Class.

        // Enable adding the functionality of INotifyPropertyChanged interface into the sample. We will use it for updating
        // information in the TextBox defined in Xaml as to whether the ArcGISTiledMapServiceLayer in the MapView Control 
        // has initialized.

        // Adding the INotifyPropertyChanged interface was performed by the developer. This interface is not automatically added 
        // as part of plumbing code. Adding in the INotifyPropertyChanged interface allows the 'LayersInitializedProperty' to 
        // receive notifications.
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        // Global variable that holds text that inform the user the state of the ArcGISTiledMapServiceLayer initialization.
        private string _layersInitialized = "No Initialized";

        // Gets or sets the text indicating whether the ArcGISTiledMapServiceLayer is initialized or not. By default the text will 
        // be: "No Initialized", later once the ArcGISTiledMapServiceLayer is initialized the text will be: "Initialized".
        public string LayersInitializedProperty
                return _layersInitialized;
                _layersInitialized = value;

                if (PropertyChanged != null)
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("LayersInitializedProperty"));

        public MainWindow()


            // Set up the Binding DataContent to this application. This action causes the LayersInitializedProperty to be invoked.
            // NOTE: the action of invoking the LayersInitializedProperty will not occur immediately when stepping thru via the 
            // debugger. The 'MainWindow' which is (this) runs on the GUI thread, needs to wait a while for CPU time to update the Binding.
            // When the debugger has an opportunity catch up on the thread the LayersInitializedProperty will have the 
            // _layersInitialized variable set to "No Initialized" and hence what is displayed in the GUI TextBox.text Property. Most
            // likely you will not see the LayersInitializedProperty Property execute until after the await line is hit in the 
            // HandleLayerInitialized function because this is when the compiler has time to process the GUI thread!
            DataContext = this;

            // Call the function that determines when all of the layers in the Map have initialized.
            System.Threading.Tasks.Task myTask = HandleLayersInitialized();


        public async System.Threading.Tasks.Task HandleLayersInitialized() 
            // This next line of code is important. The Task.WhenAll is a Microsoft function that ascertains when a Task has 
            // completed. By using a LINQ query for MapView1.Map.Layers.Select we cycle through all of the Layers to know if we have 
            // a successful layer completion, an error or a cancel.
            await System.Threading.Tasks.Task.WhenAll(MapView1.Map.Layers.Select(layer => layer.InitializeAsync()).ToArray());

            // NOTE: at this exact moment the _layersInitialized global variable equals "No Initialized" and hence the 
            // Binding in the Xaml to the TextBox.Text will display: "No Initialized".

            // Once this next line of code executes, the LayersInitializedProperty will be called again and this time the 
            // _layersInitialized global variable will be set to the text "Initialized!" and the PropertyChanged Event will
            // be raised with affect the TextBox.Text binding to display the text: "Initialized!".
            LayersInitializedProperty = "Initialized!";