Click or drag to resize
Code Example - GraphicsLayerWindow

Demonstrates various Graphics and GraphicsLayer related actions.

Code Example
Graphics Layer Window

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="GraphicsLayerWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
        Tag="GraphicsLayerWindow" >
    <Grid x:Name="LayoutRoot">
        <Grid.Resources>
            <esri:SimpleMarkerSymbol x:Key="mag2to3" Color="#FF0A57FC" Size="12" Style="Triangle" />
            <esri:SimpleMarkerSymbol x:Key="mag3to4" Color="#FF1CA2A3" Size="12" Style="Triangle" />
            <esri:SimpleMarkerSymbol x:Key="mag4to5" Color="#FF60AD12" Size="12" Style="Triangle" />
            <esri:SimpleMarkerSymbol x:Key="mag5to6" Color="#FFFFFE0A" Size="12" Style="Triangle" />
            <esri:SimpleMarkerSymbol x:Key="mag6to7" Color="#FFFDA208" Size="12" Style="Triangle" />
            <esri:SimpleMarkerSymbol x:Key="mag7to8" Color="#FFFB5107" Size="12" Style="Triangle" />
            <esri:ClassBreaksRenderer x:Key="MyClassBreaksRenderer" Field="Magnitude" >
                <esri:ClassBreaksRenderer.Infos>
                    <esri:ClassBreakInfo Minimum="2" Maximum="3" Symbol="{StaticResource mag2to3}" />
                    <esri:ClassBreakInfo Minimum="3" Maximum="4" Symbol="{StaticResource mag3to4}" />
                    <esri:ClassBreakInfo Minimum="4" Maximum="5" Symbol="{StaticResource mag4to5}" />
                    <esri:ClassBreakInfo Minimum="5" Maximum="6" Symbol="{StaticResource mag5to6}" />
                    <esri:ClassBreakInfo Minimum="6" Maximum="7" Symbol="{StaticResource mag6to7}" />
                    <esri:ClassBreakInfo Minimum="7" Maximum="8" Symbol="{StaticResource mag7to8}" />
                </esri:ClassBreaksRenderer.Infos>
            </esri:ClassBreaksRenderer>
            <esri:SimpleRenderer x:Key="SimpleRenderer">
                <esri:SimpleMarkerSymbol Color="#FFFB5107" Size="12" Style="Triangle" />
            </esri:SimpleRenderer>
        </Grid.Resources>

        <esri:MapView x:Name="MapView1" WrapAround="True">
            <esri:Map  x:Name="MyMap">
                <esri:ArcGISTiledMapServiceLayer ServiceUri="http://services.arcgisonline.com/arcgis/rest/services/world_topo_map/MapServer" />
            </esri:Map>
        </esri:MapView>

        <StackPanel Width="250" HorizontalAlignment="Right"
                    VerticalAlignment="Top" Background="Black"
                    Orientation="Vertical">
            <Button Margin="3" 
                    Content="Add Graphics Layer" Click="OnGraphicsLayerAddClicked"/>
            <Button Margin="3" 
                    Content="Add Single Graphic" Click="OnGraphicAddClicked"/>
            <Button Margin="3" 
                    Content="Switch Graphics Source" Click="OnSwitchGraphicsSourceClicked"/>
            <Button Margin="3" 
                    Content="Check Events" Click="OnCheckEventsClicked"/>
            <Button Margin="3" 
                    Content="Apply Renderer in Code" Click="OnApplyRendererClicked"/>
            <Button Margin="3" 
                    Content="Selection color" Click="OnTestSelectionColorClicked"/>
            <Button Margin="3" 
                    Content="Add Graphic Range" Click="OnAddGraphicRangeClicked"/>
            <Button Margin="3" 
                    Content="Insert Graphic Item" Click="OnInsertGraphicClicked"/>
            <Button Margin="3" 
                    Content="Clear Graphic Item" Click="OnClearGraphicItemClicked"/>

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

using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Resources;


/// <summary>
/// Demonstrates various Graphics and GraphicsLayer related actions.
/// </summary>
namespace GraphicsLayerWindow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void OnGraphicsLayerAddClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                CsvLayer csvLayer = new CsvLayer();

                //Uri uri = new Uri("./Data/earthquake_csv_data.txt", UriKind.Relative);
                //Uri uri = new Uri("pack://application:,,,/GraphicsLayerWindow;component/Data/earthquake_csv_data.txt");
                //Uri uri = new Uri("./Data/earthquake_csv_data.txt", UriKind.Relative);
                StreamResourceInfo sri = Application.GetResourceStream(new Uri("pack://application:,,,/GraphicsLayerWindow;component/Data/earthquake_csv_data.txt"));                    
                //StreamResourceInfo sri = Application.GetContentStream(uri);
                if (sri != null)
                {
                    using (Stream s = sri.Stream)
                    {
                        await csvLayer.SetSourceAsync(s);
                        csvLayer.Renderer = LayoutRoot.Resources["MyClassBreaksRenderer"] as ClassBreaksRenderer;
                        MapView1.Map.Layers.Add(csvLayer);
                        await csvLayer.InitializeAsync();
                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        private void OnGraphicAddClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                GraphicsLayer gLayer = MapView1.Map.Layers["EventsGraphicsLayer"] as GraphicsLayer;
                if (gLayer != null)
                    MapView1.Map.Layers.Remove(gLayer);

                GraphicsLayer graphicsLayer = new GraphicsLayer()
                {
                    ID = "SimpleGraphicsLayer"
                };

                graphicsLayer.Graphics.Add(new Graphic()
                {
                    Geometry = new MapPoint(-74.0064, 40.7142),
                    Symbol = new SimpleMarkerSymbol()
                    {
                        Style = SimpleMarkerStyle.Circle,
                        Color = Colors.Red,
                        Size = 8
                    }
                });
                MapView1.Map.Layers.Add(graphicsLayer);
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void OnSwitchGraphicsSourceClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                GraphicsLayer gLayer = MapView1.Map.Layers["EventsGraphicsLayer"] as GraphicsLayer;
                if (gLayer != null)
                    MapView1.Map.Layers.Remove(gLayer);

                GraphicsLayer graphicsLayer = new GraphicsLayer()
                {
                    ID = "SimpleGraphicsLayer"
                };
                if (graphicsLayer != null)
                {
                    List<Graphic> newGraphics = new List<Graphic>();
                    newGraphics.Add(new Graphic()
                    {
                        Geometry = new MapPoint(-74.0064, 40.7142),
                        Symbol = new SimpleMarkerSymbol()
                        {
                            Style = SimpleMarkerStyle.Circle,
                            Color = Colors.Yellow,
                            Size = 8
                        }
                    });

                    graphicsLayer.GraphicsSource = newGraphics;
                    MapView1.Map.Layers.Add(graphicsLayer);

                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        private void OnCheckEventsClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                GraphicsLayer gLayer = MapView1.Map.Layers["SimpleGraphicsLayer"] as GraphicsLayer;
                if (gLayer != null)
                    MapView1.Map.Layers.Remove(gLayer);

                GraphicsLayer gLayer2 = MapView1.Map.Layers["EventsGraphicsLayer"] as GraphicsLayer;
                if (gLayer2 != null)
                    MapView1.Map.Layers.Remove(gLayer2);

                GraphicsLayer graphicsLayer = new GraphicsLayer()
                {
                    ID = "EventsGraphicsLayer"
                };
                if (graphicsLayer != null)
                {
                    List<Graphic> newGraphics = new List<Graphic>();
                    newGraphics.Add(new Graphic()
                    {
                        Geometry = new MapPoint(-74.0064, 40.7142),
                        Symbol = new SimpleMarkerSymbol()
                        {
                            Style = SimpleMarkerStyle.Circle,
                            Color = Colors.Yellow,
                            Size = 8
                        }
                    });

                    graphicsLayer.GraphicsSource = newGraphics;

                    MapView1.Map.Layers.Add(graphicsLayer);

                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        private void OnApplyRendererClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                IEnumerable<GraphicsLayer> graphicLayers = MapView1.Map.Layers.OfType<GraphicsLayer>();
                List<GraphicsLayer> gLayers = new List<GraphicsLayer>();

                if (graphicLayers != null)
                {
                    foreach (GraphicsLayer l in graphicLayers)
                        gLayers.Add(l);

                    foreach (GraphicsLayer l in gLayers)
                        MapView1.Map.Layers.Remove(l);
                }

                GraphicsLayer graphicsLayer = new GraphicsLayer()
                {
                    ID = "SimpleGraphicsLayer"
                };

                graphicsLayer.Graphics.Add(new Graphic()
                {
                    Geometry = new MapPoint(-74.0064, 40.7142),

                });
                graphicsLayer.Renderer = LayoutRoot.Resources["SimpleRenderer"] as SimpleRenderer;
                graphicsLayer.RenderingMode = GraphicsRenderingMode.Static;
                graphicsLayer.SelectionColor = Colors.Yellow;
                MapView1.Map.Layers.Add(graphicsLayer);
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void OnTestSelectionColorClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                IEnumerable<GraphicsLayer> graphicLayers = MapView1.Map.Layers.OfType<GraphicsLayer>();
                List<GraphicsLayer> gLayers = new List<GraphicsLayer>();

                if (graphicLayers != null)
                {
                    foreach (GraphicsLayer l in graphicLayers)
                        gLayers.Add(l);

                    foreach (GraphicsLayer l in gLayers)
                        MapView1.Map.Layers.Remove(l);
                }

                GraphicsLayer graphicsLayer = new GraphicsLayer()
                {
                    ID = "SimpleGraphicsLayer"
                };

                graphicsLayer.Graphics.Add(new Graphic()
                {
                    Geometry = new MapPoint(-74.0064, 40.7142),
                    IsSelected = true
                });
                graphicsLayer.Renderer = LayoutRoot.Resources["SimpleRenderer"] as SimpleRenderer;
                graphicsLayer.SelectionColor = Colors.Yellow;

                MapView1.Map.Layers.Add(graphicsLayer);
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void OnAddGraphicRangeClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                IEnumerable<GraphicsLayer> graphicLayers = MapView1.Map.Layers.OfType<GraphicsLayer>();
                List<GraphicsLayer> gLayers = new List<GraphicsLayer>();

                if (graphicLayers != null)
                {
                    foreach (GraphicsLayer l in graphicLayers)
                        gLayers.Add(l);

                    foreach (GraphicsLayer l in gLayers)
                        MapView1.Map.Layers.Remove(l);
                }

                GraphicsLayer graphicsLayer = new GraphicsLayer()
                {
                    ID = "SimpleGraphicsLayer"
                };

                List<Graphic> graphics = new List<Graphic>()
                {
                    new Graphic()
                {
                    Geometry = new MapPoint(-74.0064, 40.7142),

                }
                };
                graphicsLayer.Graphics.AddRange(graphics);
                graphicsLayer.Renderer = LayoutRoot.Resources["SimpleRenderer"] as SimpleRenderer;

                MapView1.Map.Layers.Add(graphicsLayer);
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void OnInsertGraphicClicked(object sender, RoutedEventArgs e)
        {
            try
            {
                IEnumerable<GraphicsLayer> graphicLayers = MapView1.Map.Layers.OfType<GraphicsLayer>();
                List<GraphicsLayer> gLayers = new List<GraphicsLayer>();

                if (graphicLayers != null)
                {
                    foreach (GraphicsLayer l in graphicLayers)
                        gLayers.Add(l);

                    foreach (GraphicsLayer l in gLayers)
                        MapView1.Map.Layers.Remove(l);
                }

                GraphicsLayer graphicsLayer = new GraphicsLayer()
                {
                    ID = "SimpleGraphicsLayer"
                };

                graphicsLayer.Graphics.Insert(0, new Graphic()
                {
                    Geometry = new MapPoint(-74.0064, 40.7142),

                });

                graphicsLayer.Renderer = LayoutRoot.Resources["SimpleRenderer"] as SimpleRenderer;

                MapView1.Map.Layers.Add(graphicsLayer);
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void OnClearGraphicItemClicked(object sender, RoutedEventArgs e)
        {
            try
            {

                GraphicsLayer graphicsLayer = MapView1.Map.Layers["SimpleGraphicsLayer"] as GraphicsLayer;

                if (graphicsLayer != null && graphicsLayer.Graphics.Count > 0)
                {
                    graphicsLayer.Graphics.Clear();
                }


            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}