Click or drag to resize
Code Example - FeatureLayerViaGeodatabase

Demonstrates creating a FeatureLayer from a GeodatabaseFeatureTable in an ArcGIS Runtime provisioned Geodatabase that is stored locally on disk.

Code Example
Feature Layer Via Geodatabase

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="FeatureLayerViaGeodatabase.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="58" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" 
                           Width="770" TextWrapping="Wrap"  Margin="10,2,0,0" />

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

                <!-- Button to add an FeatureLayer via code-behind. -->
                <Button Content="Add FeatureLayer to Map" 
                        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="551" Height="480" VerticalAlignment="Top" Margin="2,2,2,2">
                    <!-- A Map. -->
                    <esri:Map  x:Name="Map1"/>
                </esri:MapView>

                <!-- Display the names of various FeatureLayers that can be added to the Map. -->
                <StackPanel Orientation="Vertical">
                    <ListBox Height="139" Width="224" Name="ListBox1"/>
                </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 "FeatureLayerViaGeodatabase.MainWindow" to be just "MainWindow".

// NOTE: The ArcGISRuntime content .geodatabase file that was used in this sample was obtained from the US Census at the following Url:
// http://www.census.gov/geo/maps-data/data/tiger-geodatabases.html <-- download the State Level Geodatabase for Idaho.
// The downloaded file (tlgdb_2013_a_16_id.gdb.zip) was then unzipped to reveal the ..\tlgdb_2013_a_16_id.gdb folder structure
// which is an ArcMap GDB dataset. You must load in the ..\tlgdb_2013_a_16_id.gdb database in ArcMap and then use the 
// File > Share As > Runtime content to provision the tlgdb_2013_a_16_id.geodatabase file. More detailed instructions on provisioning
// ArcGIS Runtime .geodatabase files can be found in the online help document:
// http://resources.arcgis.com/en/help/main/10.2/index.html#//00660000045q000000

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

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.Text = "When the application loads an ArcGIS Runtime Geodatabase file on local disk is read and the GeodatabaseFeatureTables it " + 
                "contains are listed in a ListBox. Click on one of the names (and associated index number) of the GeodatabaseFeatureTables in the Listbox " + 
                "and then click the 'Add FeatureLayer to Map' button to add the FeatuerLayer to the Map.";

            // Set the initial extent to that of the Idaho region.
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(-118.16,41.98,-110.11,49.00);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myEnvelope);

            // CAUTION: Local disk based FeatureLayers DO NOT re-project to match the Map's SpatialReference!
            // It is recommended to always set the SpatialReference for the Map when using a FeatureLayer based on a local disk based FeatureTable such as
            // a ShapefileTable or a GeodatabaseFeatureTable. The reason is that the visual elements of the FeatureLayer may not display if the Shapefile or
            // GeodatabaseFeatureTable are not based on the SpatialReference WKID of 3857 or 102100 (aka. Web Mercator). By specifying the Map's SpatialReference to 
            // be exactly the same at the ShapefileTable or GeodatabaseFeatureTable, you will be assured that it displays visually in the Map. 
            // NOTE: If the FeatureLayer is based on an ArcGIS Server web ServiceFeatureTable, then re-projection of the data automatically occurs 
            // to match the SpatialReference of the Map.
            MapView1.Map.SpatialReference = Esri.ArcGISRuntime.Geometry.SpatialReference.Create(4269);

            // Call a custom subroutine that opens a Geodatabase on the local device, gets the list of GeodatabaseFeatureTables, and presents them to the 
            // user in a ListBox. 
            Init();
        }

        public async void Init()
        {
            // Create a geodatabase using the Geodatabase.OpenAsync static/shared Method from a path/filename on the local device.
            Esri.ArcGISRuntime.Data.Geodatabase myGeodatabase = await Esri.ArcGISRuntime.Data.Geodatabase.OpenAsync("C:\\TestData\\tlgdb_2013_a_16_id.geodatabase");

            // Get all of the GeodatabaseFeatureTables from the geodatabase. They will be used to construct a FeatureLayer later.
            System.Collections.Generic.IEnumerable<Esri.ArcGISRuntime.Data.GeodatabaseFeatureTable> myGeodatabaseFeatureTables = myGeodatabase.FeatureTables;

            // Loop through each of the GeodatabaseFeatureTables and get its Name and ID to display that in a ListBox that user can then select and add to the Map as a FeatureLayer. 
            foreach (var oneFeatureTable in myGeodatabaseFeatureTables)
            {
                // Get the ServiceInfo that contains a bunch of metadata about the GeodatabaseFeatureTable.
                Esri.ArcGISRuntime.ArcGISServices.FeatureServiceLayerInfo myServiceInfo = oneFeatureTable.ServiceInfo;

                // Get the name of the GeodatabaseFeatureTable.
                string myTableName = myServiceInfo.Name;

                // Get the ID of the GeodatabaseFeatureTable.
                int myID = myServiceInfo.ID;

                ListBox1.Items.Add(myTableName + ": " + myID.ToString());
            }
        }

        private async void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Clear out any existing layers in the Map.
            MapView1.Map.Layers.Clear();

            // Get the user choice from the ListBox (it contains a string with the GeodatabaseFeatureTable Name and ID).
            string myGeodatabaseFeatureTableNameAndId = (string)ListBox1.SelectedValue;

            // Get the Name of the GeodatabaseFeatureTable.
            string myGeodatabaseFeatureTableName = myGeodatabaseFeatureTableNameAndId.Split(':')[0];

            // Get the ID of the GeodatabaseFeatureTable.
            int myGeodatabaseFeatureTableID = System.Convert.ToInt32(myGeodatabaseFeatureTableNameAndId.Split(':')[1]);

            // Create a geodatabase using the Geodatabase.OpenAsync static/shared Method from a path/filename on the local device.
            Esri.ArcGISRuntime.Data.Geodatabase myGeodatabase = await Esri.ArcGISRuntime.Data.Geodatabase.OpenAsync("C:\\TestData\\tlgdb_2013_a_16_id.geodatabase");

            // Create a new GeodatabaseFeatureTable based upon its ID.
            Esri.ArcGISRuntime.Data.GeodatabaseFeatureTable myGeodatabaseFeatureTable = myGeodatabase.GetFeatureTableById(myGeodatabaseFeatureTableID);

            // Create a new FeatureLayer based upon a GeodatabaseFeatureTable and also set its DisplayName and ID values.
            Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = new Esri.ArcGISRuntime.Layers.FeatureLayer(myGeodatabaseFeatureTable);
            myFeatureLayer.ID = myGeodatabaseFeatureTable.Name;
            myFeatureLayer.DisplayName = myGeodatabaseFeatureTable.Name;

            // Add the FeatureLayer to the Map.
            MapView1.Map.Layers.Add(myFeatureLayer);
        }    
    }
}