Click or drag to resize
Code Example - FeatureLayer_SetFeatureVisible

Demonstrates being able to turn off the visibility of a feature in a FeatureLayer by tapping on it.

Code Example
Feature Layer Set Feature Visible

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

            <!-- 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 Orientation="Horizontal">

                <!-- A MapView Control to display various GIS layers. -->
                <esri:MapView x:Name="MapView1" Width="662" Height="507" VerticalAlignment="Top" Margin="2,2,2,2" MapViewTapped="MapView1_MapViewTapped">

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

                        <!-- Add a backdrop ArcGISTiledMapServiceLayer-->
                        <esri:ArcGISTiledMapServiceLayer ID="myArcGISTiledMapServiceLayer" 
                              ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" />
                    </esri:Map>
                </esri:MapView>
                <StackPanel Orientation="Vertical">
                    <Label x:Name="Lable1" Content="Invisible Feature IDs:" />
                    <TextBlock x:Name="TextBlock_IDs" Width="118" Height="234" Background="LightYellow"/>
                    <Button x:Name="Reset" Content="Reset" Click="Reset_Click"/>
                </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 "FeatureLayer_SetFeatureVisible.MainWindow" to be just "MainWindow".

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

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.Text = "When the application loads, tap on various states to turn off their visibility in the FeatureLayer. The states that have their " + 
                "Visibility turned off will display their ID values in the TextBlock. Click the 'Reset' button to have those Features with their visibility turned off " + 
                "be turned back on again ";

            // Define the Uri to the ArcGIS Server web service that hosts a FeatureLayer. 
            System.Uri myUri = new System.Uri("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");

            // Create a new FeatureLayer from the Uri and provide an ID for it. 
            Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = new Esri.ArcGISRuntime.Layers.FeatureLayer(myUri);
            myFeatureLayer.ID = "US_States";

            // Get the ServiceFeatureTable from the FeatureLayer and make sure that all attribute fields can be returned from a query.  
            Esri.ArcGISRuntime.Data.ServiceFeatureTable myServiceFeatureTable = (Esri.ArcGISRuntime.Data.ServiceFeatureTable)myFeatureLayer.FeatureTable;
            myServiceFeatureTable.OutFields = Esri.ArcGISRuntime.Tasks.Query.OutFields.All;

            // Define a SimpleFillSymbol to render polygons: solid blue with black outline.
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol myPolygonSymbol = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            myPolygonSymbol.Color = System.Windows.Media.Colors.Blue;
            myPolygonSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            Esri.ArcGISRuntime.Symbology.SimpleLineSymbol myPolylineSymbol = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
            myPolylineSymbol.Color = System.Windows.Media.Colors.Black;
            myPolylineSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid;
            myPolygonSymbol.Outline = myPolylineSymbol;

            // Create a SimpleRenderer from the SimpleFillSymbol.
            Esri.ArcGISRuntime.Symbology.SimpleRenderer myRenderer = new Esri.ArcGISRuntime.Symbology.SimpleRenderer();
            myRenderer.Symbol = myPolygonSymbol;

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

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

            // Set the initial extent of the Map (via the InitialViewpoint Property) to be the North-East US.
            Esri.ArcGISRuntime.Geometry.Geometry myGeometry = new Esri.ArcGISRuntime.Geometry.Envelope(-9453251, 4249734, -7115541, 6119901);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myGeometry);
        }

        // Member (aka. global) variable to hold those feature RowIDs that have been made invisible.
        public System.Collections.Generic.List<long> _InvisibleFeatureIDs = new System.Collections.Generic.List<long>();

        private async void MapView1_MapViewTapped(object sender, Esri.ArcGISRuntime.Controls.MapViewInputEventArgs e)
        {
            try
            {
                // Get the FeatureLayer from the Map. 
                Esri.ArcGISRuntime.Controls.Map myMap = MapView1.Map;
                Esri.ArcGISRuntime.Layers.LayerCollection myLayerCollection = myMap.Layers;
                Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = (Esri.ArcGISRuntime.Layers.FeatureLayer)myLayerCollection[1];

                // Use the FeatureLayer.HitTestAsync Method to retrieve the feature (actually the FeatureLayer ID) that intersects the point that was 
                // clicked/tapped on the Map by the user. By leaving off the optional last parameter, the HitTestAsync return a single record; this is  
                // perfect for doing an Identify type of operation. 
                long[] myFeatureLayerRowIDs = await myFeatureLayer.HitTestAsync(MapView1, e.Position); //e.Position is in Windows device screen points.

                if (myFeatureLayerRowIDs.Length == 1)
                {
                    // Turn off the visibility of the tapped feature via it's RowID value. 
                    myFeatureLayer.SetFeatureVisibility(myFeatureLayerRowIDs, false);

                    // Display the RowID of the feature with is visibility being turned off. 
                    TextBlock_IDs.Text = TextBlock_IDs.Text + myFeatureLayerRowIDs[0].ToString() + System.Environment.NewLine;

                    // Add the RowID to a member (i.e global) variable into a List whose features have been made invisible. 
                    _InvisibleFeatureIDs.Add(myFeatureLayerRowIDs[0]);
                }
            }
            catch (System.Threading.Tasks.TaskCanceledException)
            {
                // This exception occurred because the user has already clicked the button but has not clicked/tapped a MapPoint on the Map yet.
                System.Windows.MessageBox.Show("Click or tap on over a feature in the map to changes its visibility.");
            }
            catch (System.Exception ex)
            {
                // We had some kind of issue. Display to the user so it can be corrected.
                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private void Reset_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Get the FeatureLayer from the Map. 
            Esri.ArcGISRuntime.Controls.Map myMap = MapView1.Map;
            Esri.ArcGISRuntime.Layers.LayerCollection myLayerCollection = myMap.Layers;
            Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = (Esri.ArcGISRuntime.Layers.FeatureLayer)myLayerCollection[1];

            // Turn on the visibility of those features that were turned off.
            myFeatureLayer.SetFeatureVisibility(_InvisibleFeatureIDs, true);

            // Clear the List of RowID values.
            _InvisibleFeatureIDs.Clear();

            // Clear the TextBlock of the RowID values displayed to the user.
            TextBlock_IDs.Text = "";
        }
    }
}