Click or drag to resize
Code Example - MapView_LayersLoadedAsync

Demonstrates how to determine if a particular layer has loaded via the MapView_LayersLoadedAsync Method.

Code Example
Map View Layers Loaded Async

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_LayersLoadedAsync.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 x:Name="LayoutRoot" Background="Black">

        <!-- Add a default MapView. Adjust the margin of the MapView to be able to see the background of the Grid that houses the MapView. -->
        <esri:MapView x:Name="MapView1" Height="463" Margin="30,30,30,30" >

            <!-- 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="Cities">
                    <esri:FeatureLayer.FeatureTable>
                        <esri:ServiceFeatureTable ServiceUri="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0"/>
                    </esri:FeatureLayer.FeatureTable>
                </esri:FeatureLayer>

            </esri:Map>
        </esri:MapView>
    </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_LayersLoadedAsync.MainWindow" to be just "MainWindow".

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

            // Zoom to the interior of the east central US.
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(-90, 35, -85, 40);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myEnvelope);

            // Call the custom function that will determine when all of the layers have loaded in the Map.
            var _x = AreAllLayersLoaded();
        }

        public async System.Threading.Tasks.Task AreAllLayersLoaded()
        {
            // This custom function is asynchronous and attempts to load all of the layers in the Map.

            try
            {
                // Awaitable function that tries to load all of the layers in the Map. All program execution on this
                // thread stops until all the layers load or error out.
                await MapView1.LayersLoadedAsync();
            }
            catch (System.Exception ex)
            {
                // There was an error with one or more of the layers loading. Display that message to the user.
                System.Windows.MessageBox.Show(ex.ToString());
            }

            // Now that all of the layers have either loaded or errored-out. Display a MessageBox to notify the users that
            // the background of the Grid that hold the Map Control will be changing to Red. 
            System.Windows.MessageBox.Show("Changing the background color of the Grid to Red", "All layers attempted to load");

            // Change the Grids background color.
            LayoutRoot.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
        }


    }
}