Best practices: Graphics layers, symbols, and renderers

Using the ArcGIS API for Silverlight, you can dynamically display graphics on a map through a Graphics layer. A Graphics layer could, for example, be used to hold polygons drawn by a user or display features that satisfy a user-defined query.

Symbols define all the non-geographic aspects of a graphic's appearance. This includes a graphic's color, border width, transparency, and more. The ArcGIS API for Silverlight includes many symbol classes, each of which allows you to specify symbology in a unique way. Each symbol type is also specific to one geometry type (that is, point, line, or polygon).

Renderers define one or more symbols to apply to a Graphics layer. The symbol applied to each graphic depends on the graphic's attributes. The renderer specifies which attribute values correspond to which symbol.

The following sections detail some of the best practices for working with a GraphicsLayer, symbols, and renderers.

Set the GraphicsLayer.Renderer or GraphicsLayer.RendererTakesPrecedence properties

By default, the Viewer expects the GraphicsLayer.Renderer property to be used to set the layer’s symbology. If the Renderer property is not set when a GraphicsLayer is added to the map, the Viewer will automatically populate this property with a default renderer; therefore, you should set the GraphicsLayer.Renderer property before the layer is added to the map. Sample code for setting the Renderer is as follows:

GraphicsLayer gl = new GraphicsLayer()
{
    ID = "IdentifyResultsLayer",
    Renderer = new SimpleRenderer()
    {
          Symbol = identifyDialog.Resources["RedMarkerSymbol"] as Symbol
    }
};

Alternatively, you may set the GraphicsLayer.RendererTakesPrecedence property to false, then set the Symbol property of each Graphic within the layer. When RendererTakesPrecedence is set to false, Graphic.Symbol will override any symbology defined by GraphicsLayer.Renderer. If using this approach, both RendererTakesPrecedence and Graphic.Symbol should be set before adding the layer to the map.

Supported renderers

Only UniqueValueRenderer, ClassBreaksRenderer, and SimpleRenderer are currently supported for persistence and configuration. If an unsupported renderer type is used, users will not be able to configure the symbology of the results layer after it has been added to the map. As an example, consider a tool that adds a GraphicsLayer to the map. It is possible for users of the Application Builder to run such a tool while they are editing a Viewer application. If they use a supported renderer type, users can configure the symbology of the results layer after it has been added to the map. Additionally, if they save or deploy that application, the results layer will be stored as part of that application’s map. These capabilities will not be available on GraphicsLayers that use renderer types that do not support persistence and configuration (for example, TemporalRenderer).

If a renderer type other than UniqueValueRenderer, ClassBreaksRenderer, or SimpleRenderer is used, do not allow the tool to be executed from within the Application Builder. This is because renderer types other than the ones listed do not support persistence and configuration. To do this, check whether Application.Current.IsEditMode is true, and, if so, prevent the tool from being run.

GraphicsLayer symbols

Declare the symbols used in your GraphicsLayer in a resource collection within your UI’s Extensible Application Markup Language (XAML). While symbols can be created programmatically, declaring them in XAML is more concise, readable, and generally easier to maintain.

<UserControl.Resources>
   <ResourceDictionary>
       <esri:SimpleMarkerSymbol x:Key="RedMarkerSymbol" Color="Red" Size="12" Style="Circle" />
   </ResourceDictionary>
</UserControl.Resources>

Layer name

To retrieve or set the layer name that is shown in the Map Contents panel, use MapApplication.LayerNameProperty. You can set the name by calling Layer.SetValue(MapApplication.LayerNameProperty, “layer name”), and retrieve the name by calling Layer.GetValue(MapApplication.LayerNameProperty).

GraphicsLayer gl = new GraphicsLayer();
gl.SetValue(MapApplication.LayerNameProperty, "Identify Results");
gl.GetValue(MapApplication.LayerNameProperty);
1/26/2015