Simple Renderer

Download Samples Repository

Description

Sample shows how to create a SimpleRenderer for a graphics layer in XAML and code. User added points will be added to the graphics layer and rendered using the GraphicsLayer Renderer.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.SimpleRendererSample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:ArcGISRuntime.Samples.Desktop"
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
    <Grid x:Name="layoutGrid">
        <esri:MapView x:Name="MyMapView" WrapAround="True">
			<esri:Map InitialViewpoint="-15053000,2749000,-6540000,6590000">
				<esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="graphicsOverlay">
					<esri:GraphicsOverlay.Renderer>
						<esri:SimpleRenderer>
							<esri:SimpleMarkerSymbol Color="Red" Style="Circle" Size="15" />
						</esri:SimpleRenderer>
					</esri:GraphicsOverlay.Renderer>
				</esri:GraphicsOverlay>
			</esri:MapView.GraphicsOverlays>
        </esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1" Margin="30" Padding="20"
                HorizontalAlignment="Right" VerticalAlignment="Top">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
			<StackPanel>
				<TextBlock Text="Click the map to add points with the current default renderer symbol. The 'Change Renderer' button will change the renderer to a new SimpleRenderer and redraw the map."
                           FontSize="14"  Width="400" TextAlignment="Left" TextWrapping="Wrap" />
                
                <Button Content="Change Renderer" HorizontalAlignment="Center" Margin="12,12,12,0"
                        Click="ChangeRendererButton_Click"/>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// Sample shows how to create a SimpleRenderer for a graphics layer in XAML and code. User added points will be added to the graphics layer and rendered using the GraphicsLayer Renderer.
	/// </summary>
	/// <title>Simple Renderer</title>
	/// <category>Symbology</category>
	public partial class SimpleRendererSample : UserControl
	{
		private Random _random = new Random();
		private GraphicsOverlay _graphicsOverlay;

		/// <summary>Construct Simple Renderer sample control</summary>
		public SimpleRendererSample()
		{
			InitializeComponent();

			_graphicsOverlay = MyMapView.GraphicsOverlays["graphicsOverlay"];

			MyMapView.ExtentChanged += MyMapView_ExtentChanged;
		}

		// Start map interaction
		private async void MyMapView_ExtentChanged(object sender, EventArgs e)
		{
			MyMapView.ExtentChanged -= MyMapView_ExtentChanged;
			await AcceptPointsAsync();
		}

		// Change the graphics layer renderer to a new SimpleRenderer
		private void ChangeRendererButton_Click(object sender, RoutedEventArgs e)
		{
			_graphicsOverlay.Renderer = new SimpleRenderer() { Symbol = GetRandomSymbol() };
		}

		// Accept user map clicks and add points to the graphics layer (use the default symbol from renderer)
		private async Task AcceptPointsAsync()
		{
			try
			{
				while (MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry.Extent != null)
				{
					var point = await MyMapView.Editor.RequestPointAsync();
					_graphicsOverlay.Graphics.Add(new Graphic(point));
				}
			}
			catch (TaskCanceledException) { }
			catch (Exception ex)
			{
				MessageBox.Show("Error: " + ex.Message, "Simple Renderer Sample");
			}
		}

		// Utility: Generate a random simple marker symbol
		private SimpleMarkerSymbol GetRandomSymbol()
		{
			return new SimpleMarkerSymbol()
			{
				Size = 15,
				Color = GetRandomColor(),
				Style = (SimpleMarkerStyle)_random.Next(0, 6)
			};
		}
		
		// Utility function: Generate a random System.Windows.Media.Color
		private Color GetRandomColor()
		{
			var colorBytes = new byte[3];
			_random.NextBytes(colorBytes);
			return Color.FromRgb(colorBytes[0], colorBytes[1], colorBytes[2]);
		}
	}
}
Feedback on this topic?