Click or drag to resize
Code Example - FeatureLayerXamlAndCodeBehind

Shows creating a FeatureLayer in XAML and via code-behind. The FeatureLayer.InitializeAsync Method is used to obtain metadata about the layer (i.e. ID and Copyright) using the Async/Task framework.

Code Example
Feature Layer Xaml And Code Behind

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

        <!-- Create a simple renderer with a red, solid polygon fill. The outline of the polygon is black. -->
        <Grid.Resources>
            <esri:SimpleRenderer x:Key="mySimpleRenderer_Red">
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleFillSymbol Color="Red"  Style="Solid">
                        <esri:SimpleFillSymbol.Outline>
                            <esri:SimpleLineSymbol Color="Black" Width="1" Style="Solid"/>
                        </esri:SimpleFillSymbol.Outline>
                    </esri:SimpleFillSymbol>
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>
        </Grid.Resources>

        <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 another FeatureLayer (via code-behind) for the specified Url." 
                        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="400" Height="480" VerticalAlignment="Top" Margin="2,2,2,2">

                    <!-- A Map. -->
                    <esri:Map  x:Name="Map1" >

                        <!-- 
                        Add a FeatureLayer. Define the renderer for the polygons. Set the ServiceFeatureTable ServiceUri to 
                        public ArcGIS Server web service. Set the Where clause to restrict the data coming back to just a few states.
                        -->
                        <esri:FeatureLayer ID="TX_OK_AZ_NM" Renderer="{StaticResource mySimpleRenderer_Red}">
                            <esri:FeatureLayer.FeatureTable>
                                <esri:ServiceFeatureTable 
                                    ServiceUri="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" 
                                    Where="STATE_ABBR = 'TX' OR STATE_ABBR = 'OK' OR STATE_ABBR = 'AZ' OR STATE_ABBR = 'NM'"/>
                            </esri:FeatureLayer.FeatureTable>
                        </esri:FeatureLayer>

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

                <StackPanel Orientation="Vertical">

                    <!-- TextBox to display information about the various layers in the Map. -->
                    <TextBox x:Name="TextBox1" TextWrapping="Wrap" Height="480" Width="370" Margin="2,2,2,2"/>

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

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

            // Add any initialization after the InitializeComponent() call.

            // Provide instructions on how to use the sample. 
            TextBlock1.Text = "When the application loads a FeatureLayer will " + 
                                      "automatically be added to the Map (it was specified in XAML). Click the Button to add another " + 
                                      "FeatureLayer to the Map (it will be added via code-behind). The ID  and copyright " + 
                                      "information of each layer will displayed in the TextBox.";

            // Set the initial extent of the Map to show features from both FeatureLayers.
            Esri.ArcGISRuntime.Geometry.SpatialReference mySpatialReference = new Esri.ArcGISRuntime.Geometry.SpatialReference(4269);
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = new Esri.ArcGISRuntime.Geometry.Envelope(-122.05, 23.17, -83.29, 52.24, mySpatialReference);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myGeometry);

            // Call our custom 'async' function to display information about the FeatureLayer that was added in XAML.
            Init();
        }

        public async void Init()
        {
            // This function will display information about the FeatureLayer that was added in XAML.

            // Get the FeatureLayer by it's index number.        
            Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = (Esri.ArcGISRuntime.Layers.FeatureLayer)MapView1.Map.Layers[0];

            // Initialize the FeatureLayer which allows for obtain various Property information.
            await myFeatureLayer.InitializeAsync();

            // Get the ID of the FeatureLayer.
            string myID = myFeatureLayer.ID;

            // Create a StringBuilder object to hold information about the FeatureLayer.
            System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();

            // Add the ID of the FeatureLayer to the StringBuilder.
            myStringBuilder.Append("The 1st FeatureLayer was added via XAML and it's ID is: " + myID + System.Environment.NewLine);

            // Get the ServiceFeatureTable from the FeatureLayer.
            Esri.ArcGISRuntime.Data.ServiceFeatureTable myServiceFeatureTable = (Esri.ArcGISRuntime.Data.ServiceFeatureTable)myFeatureLayer.FeatureTable;

            // Get the FeatureServiceLayerInfo from the ServiceFeatureTable
            Esri.ArcGISRuntime.ArcGISServices.FeatureServiceLayerInfo myFeatureServiceLayerInfo = myServiceFeatureTable.ServiceInfo;

            // Get the copyright information about the FeatureLayer and add to the StringBuilder.
            string myCopyright = myFeatureServiceLayerInfo.CopyrightText;
            myStringBuilder.Append(myCopyright + System.Environment.NewLine);

            // Display the results of the StringBuilder text to the user.
            TextBox1.Text = myStringBuilder.ToString();
        }

        private async void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // This function will add an FeatureLayer to the Map via code behind.

            // Create a new Uri that point to an FeatureLayer. It is the same FeatureLayer that was added via XAML but we will select a different
            // set of features (show later).
            System.Uri myUri = new System.Uri("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");

            // Create a new ServiceFeatureTable and set it's ServiceUri and Where clause Properties.
            Esri.ArcGISRuntime.Data.ServiceFeatureTable myServiceFeatureTable = new Esri.ArcGISRuntime.Data.ServiceFeatureTable();
            myServiceFeatureTable.ServiceUri = myUri.ToString();
            myServiceFeatureTable.Where = "STATE_ABBR = 'ND' OR STATE_ABBR = 'SD' OR STATE_ABBR = 'MT' OR STATE_ABBR = 'WY'";

            // Create a new instance of an FeatureLayer and set it's Id and FeatureTable Properties.
            Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = new Esri.ArcGISRuntime.Layers.FeatureLayer();
            myFeatureLayer.ID = "ND_SD_MT_WY";
            myFeatureLayer.FeatureTable = myServiceFeatureTable;

            // Define an outline Symbol for the Fills.
            Esri.ArcGISRuntime.Symbology.SimpleLineSymbol myBlackSolidOutline = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
            myBlackSolidOutline.Color = System.Windows.Media.Colors.Black;
            myBlackSolidOutline.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid;
            myBlackSolidOutline.Width = 1;

            // Define a simple polygon fill symbol (solid, blue, with black outline).
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol_Blue = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol_Blue.Color = System.Windows.Media.Colors.Blue;
            mySimpleFillSymbol_Blue.Outline = myBlackSolidOutline;
            mySimpleFillSymbol_Blue.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;

            // Create a SimpleRenderer to hold the SimpleFillSymbol.
            Esri.ArcGISRuntime.Symbology.SimpleRenderer myRenderer = new Esri.ArcGISRuntime.Symbology.SimpleRenderer();
            myRenderer.Symbol = mySimpleFillSymbol_Blue;

            // Apply the SimpleRenderer to the FeatureLayer.
            myFeatureLayer.Renderer = myRenderer;

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

            // Initialize the FeatureLayer which allows for obtain various Property information.
            await myFeatureLayer.InitializeAsync();

            // Get the ID of the FeatureLayer.
            string myID = myFeatureLayer.ID;

            // Create a StringBuilder object to hold information about the FeatureLayer.
            System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();
            myStringBuilder.Append(TextBox1.Text + System.Environment.NewLine);
            myStringBuilder.Append(System.Environment.NewLine);

            // Add the ID of the FeatureLayer to the StringBuilder.
            myStringBuilder.Append("The 2nd FeatureLayer was added via code-behind and it's ID is: " + myID + System.Environment.NewLine);

            // Get the FeatureServiceLayerInfo from the ServiceFeatureTable.
            Esri.ArcGISRuntime.ArcGISServices.FeatureServiceLayerInfo myFeatureServiceLayerInfo = myServiceFeatureTable.ServiceInfo;

            // Get the copyright information about the FeatureLayer and add to the StringBuilder.
            string myCopyright = myFeatureServiceLayerInfo.CopyrightText;
            myStringBuilder.Append(myCopyright + System.Environment.NewLine);

            // Display the results of the StringBuilder text to the user.
            TextBox1.Text = myStringBuilder.ToString();
        }
    }
}