Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Read symbols from a mobile style

This code sample is available for these platforms:
View Sample on GitHub

Open a mobile style (.stylx) and read its contents. Combine several symbols from the style into a single multilayer point symbol, then use it to display graphics in the map view.

image

Use case

Multilayer symbols provide the ability to create much more complex and interesting symbology for geoelements. While these symbols can be created from scratch, a more convenient workflow is to author them using ArcGIS Pro and store them in a mobile style file (.stylx). ArcGIS Runtime can read symbols from a mobile style, and you can modify and combine them as needed in your app.

How to use the sample

  • Select symbols from each list box to create a face emoji. A preview of the symbol is updated as you make selections.
  • You can optionally select a new color and set the symbol size using the slider.
  • Tap the map to create a point graphic that uses the current face symbol.
  • Tap the Clear button to clear all graphics from the display.

How it works

  1. On startup, read a mobile style file using SymbolStyle.OpenAsync.
  2. Get a list of all symbols in the style by calling SearchSymbolsAsync with the default search parameters.
  3. Iterate the list of SymbolStyleSearchResult and add symbols to list boxes according to their category. Display a preview of each symbol with CreateSwatchAsync.
  4. When symbol selections change, create a new multilayer symbol by passing the keys for the selected symbols into GetSymbolAsync. Color lock all symbol layers except the base layer and update the current symbol preview image.
  5. Create graphics symbolized with the current symbol when the user taps the map view.

Relevant API

  • MultilayerPointSymbol
  • MultilayerSymbol.CreateSwatchAsync
  • SymbolLayer
  • SymbolStyle
  • SymbolStyle.GetSymbolAsync
  • SymbolStyleSearchParameters

Offline Data

A mobile style file (created using ArcGIS Pro) provides the symbols used by the sample.

Emoji mobile style

Tags

advanced symbology, multilayer, mobile style, stylx

Sample Code

<UserControl 
    x:Class="ArcGISRuntime.UWP.Samples.SymbolsFromMobileStyle.SymbolsFromMobileStyle"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ArcGISRuntime.UWP.Samples.SymbolsFromMobileStyle"
    xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls">
    <UserControl.Resources>
        <local:ColorToSolidBrushConverter x:Key="ColorToBrushConverter"/>
    </UserControl.Resources>
    <Grid>
        <esriUI:MapView x:Name="MyMapView"/>
        <Border Style="{StaticResource BorderStyle}" 
                Padding="0" Width="400" Height="290" 
                Margin="0,20,20,0">
            <Grid Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="140"/>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="40"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <ListBox x:Name="EyeSymbolList"
                         Grid.Row="0" Grid.Column="0"
                         Margin="20"
                         BorderThickness="1"
                         SelectionMode="Single"
                         SelectionChanged="SymbolPropertyChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Image Source="{Binding ImageSrc}"
                                   Height="15"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <ListBox x:Name="MouthSymbolList"
                         Grid.Row="0" Grid.Column="1"
                         Margin="20"
                         BorderThickness="1"
                         SelectionMode="Single"
                         SelectionChanged="SymbolPropertyChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Image Source="{Binding ImageSrc}"
                                   Height="15"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <ListBox x:Name="HatSymbolList"
                         Grid.Row="0" Grid.Column="2"
                         Margin="20"
                         BorderThickness="1"
                         SelectionMode="Single"
                         SelectionChanged="SymbolPropertyChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Image Source="{Binding ImageSrc}"
                                   Height="20"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <Slider x:Name="SizeSlider"
                        SmallChange="1" LargeChange="5"
                        Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
                        Margin="20,0"
                        Minimum="8" Maximum="60" Value="20"/>
                <TextBlock x:Name="SizeLabel" 
                           Grid.Row="1" Grid.Column="2"
                           VerticalAlignment="Center">
                    <Run Text="Size:"/>
                    <Run Text="{Binding ElementName=SizeSlider, Path=Value}"/>
                </TextBlock>
                <ComboBox x:Name="FaceColorComboBox"
                          Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
                          Width="150" Height="35"
                          HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center"
                          SelectionChanged="SymbolPropertyChanged">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <Border Background="{Binding Converter={StaticResource ColorToBrushConverter}}" Height="35" Width="150"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
                <Image x:Name="SymbolPreviewImage"
                       Grid.Row="2" Grid.Column="3" Grid.RowSpan="2"
                       Width="80" Height="80"/>
                <Button x:Name="ClearGraphicsButton"
                        Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"
                        Width="150" Height="35"
                        HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center"                          
                        Content="Clear graphics"
                        Click="ClearGraphicsClick"/>
            </Grid>
        </Border>
    </Grid>
</UserControl>
// Copyright 2019 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an 
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific 
// language governing permissions and limitations under the License.

using ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Controls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Symbol = Esri.ArcGISRuntime.Symbology.Symbol;

namespace ArcGISRuntime.UWP.Samples.SymbolsFromMobileStyle
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Read symbols from a mobile style",
        "Symbology",
        "Open a local mobile style file (.stylx) and read its contents.",
        "")]
    [ArcGISRuntime.Samples.Shared.Attributes.OfflineData("1bd036f221f54a99abc9e46ff3511cbf")]
    public partial class SymbolsFromMobileStyle
    {
        // A mobile style containing symbols.
        private SymbolStyle _emojiStyle;

        // The unique identifier (key) for the background face symbol in the mobile style.
        private readonly string _baseSymbolKey = "Face1";

        public SymbolsFromMobileStyle()
        {
            InitializeComponent();

            // Call a function that will create the map and fill UI controls with symbols in a mobile style.
            Initialize();
        }

        private async Task Initialize()
        {
            // Create a new topographic basemap and assign it to the map view.
            Map map = new Map(Basemap.CreateTopographic());
            MyMapView.Map = map;

            // Create a graphics overlay for showing point graphics and add it to the map view.
            GraphicsOverlay overlay = new GraphicsOverlay();
            MyMapView.GraphicsOverlays.Add(overlay);

            // Fill the symbol color combo box with some color choices.
            List<Color> faceColors = new List<Color>
            {
                Color.Yellow, Color.LightGreen, Color.LightPink
            };
            FaceColorComboBox.ItemsSource = faceColors;
            FaceColorComboBox.SelectedIndex = 0;

            // Get the full path to the downloaded mobile style file (.stylx).
            string mobileStyleFilePath = DataManager.GetDataFolder("1bd036f221f54a99abc9e46ff3511cbf", "emoji-mobile.stylx");

            // Call a function that will read the mobile style file and populate list boxes with symbol layers.
            await ReadMobileStyle(mobileStyleFilePath);

            // Handle the tapped event on the map view to draw point graphics with the chosen symbol.
            MyMapView.GeoViewTapped += GeoViewTapped;
        }

        private async Task ReadMobileStyle(string stylePath)
        {
            try
            {
                // Open the mobile style file at the provided path.
                _emojiStyle = await SymbolStyle.OpenAsync(stylePath);

                // Get the default style search parameters.
                SymbolStyleSearchParameters searchParams = await _emojiStyle.GetDefaultSearchParametersAsync();

                // Search the style with the default parameters to return all symbol results.
                IList<SymbolStyleSearchResult> styleResults = await _emojiStyle.SearchSymbolsAsync(searchParams);

                // Create an empty placeholder image to represent "no symbol" for each category.
                ImageSource emptyImage = null;

                // Create lists to contain the available symbol layers for each category of symbol and add an empty entry as default.
                List<SymbolLayerInfo> eyeSymbolInfos = new List<SymbolLayerInfo> { new SymbolLayerInfo("", emptyImage, "") };
                List<SymbolLayerInfo> mouthSymbolInfos = new List<SymbolLayerInfo> { new SymbolLayerInfo("", emptyImage, "") };
                List<SymbolLayerInfo> hatSymbolInfos = new List<SymbolLayerInfo>() { new SymbolLayerInfo("", emptyImage, "") };

                // Loop through the results and put symbols into the appropriate list according to category.
                foreach (SymbolStyleSearchResult result in styleResults)
                {
                    // Get the symbol for this result.
                    MultilayerPointSymbol multiLayerSym = result.Symbol as MultilayerPointSymbol;

                    // Create a swatch image from the symbol.
                    RuntimeImage swatch = await multiLayerSym.CreateSwatchAsync();
                    ImageSource symbolImage = await swatch.ToImageSourceAsync();

                    // Create a symbol layer info object to represent the symbol in the list.
                    // The symbol key will be used to retrieve the symbol from the style.
                    SymbolLayerInfo symbolInfo = new SymbolLayerInfo(result.Name, symbolImage, result.Key);

                    // Add the symbol layer info to the correct list for its category.
                    switch (result.Category)
                    {
                        case "Eyes":
                            {
                                eyeSymbolInfos.Add(symbolInfo);
                                break;
                            }
                        case "Mouth":
                            {
                                mouthSymbolInfos.Add(symbolInfo);
                                break;
                            }
                        case "Hat":
                            {
                                hatSymbolInfos.Add(symbolInfo);
                                break;
                            }
                    }
                }

                // Show the symbols in the category list boxes.
                EyeSymbolList.ItemsSource = eyeSymbolInfos;
                MouthSymbolList.ItemsSource = mouthSymbolInfos;
                HatSymbolList.ItemsSource = hatSymbolInfos;

                // Call a function to construct the current symbol (default yellow circle).
                Symbol faceSymbol = await GetCurrentSymbol();

                // Call a function to show a preview image of the symbol.
                await UpdateSymbolPreview(faceSymbol);
            }
            catch (Exception ex)
            {
                // Report the exception.
                MessageDialog dialog = new MessageDialog("Error reading symbols from style: " + ex.Message);
                await dialog.ShowAsync();
            }
        }

        // Handler for the tapped event on the map view.
        private async void GeoViewTapped(object sender, GeoViewInputEventArgs e)
        {
            // Call a function to get the currently defined multilayer point symbol.
            MultilayerPointSymbol faceSymbol = await GetCurrentSymbol();

            // Create a graphic for the tapped location using the current symbol and add it to the map view.
            Graphic graphic = new Graphic(e.Location, faceSymbol);
            MyMapView.GraphicsOverlays.First().Graphics.Add(graphic);
        }

        // An event handler for list box and combo box selection changes that will update the current symbol.
        private async void SymbolPropertyChanged(object sender, SelectionChangedEventArgs e)
        {
            // Call a function that will construct the current symbol.
            Symbol faceSymbol = await GetCurrentSymbol();

            // Call a function to update the symbol preview.
            await UpdateSymbolPreview(faceSymbol);
        }

        private async Task<MultilayerPointSymbol> GetCurrentSymbol()
        {
            // If the style hasn't been opened, return.
            if (_emojiStyle == null) { return null; }

            MultilayerPointSymbol faceSymbol = null;
            try
            {
                // Get the key that identifies the selected eye symbol (or an empty string if none selected).
                SymbolLayerInfo eyeLayerInfo = (SymbolLayerInfo)EyeSymbolList.SelectedItem;
                string eyeLayerKey = eyeLayerInfo != null ? eyeLayerInfo.Key : string.Empty;

                // Get the key that identifies the selected mouth symbol (or an empty string if none selected).
                SymbolLayerInfo mouthLayerInfo = (SymbolLayerInfo)MouthSymbolList.SelectedItem;
                string mouthLayerKey = mouthLayerInfo != null ? mouthLayerInfo.Key : string.Empty;

                // Get the key that identifies the selected hat symbol (or an empty string if none selected).
                SymbolLayerInfo hatLayerInfo = (SymbolLayerInfo)HatSymbolList.SelectedItem;
                string hatLayerKey = hatLayerInfo != null ? hatLayerInfo.Key : string.Empty;

                // Create a list of the symbol keys that identify the selected symbol layers, including the base (circle) symbol.
                List<string> symbolKeys = new List<string>
                {
                    _baseSymbolKey, eyeLayerKey, mouthLayerKey, hatLayerKey
                };

                // Get a multilayer point symbol from the style that contains the selected symbol layers.
                faceSymbol = await _emojiStyle.GetSymbolAsync(symbolKeys) as MultilayerPointSymbol;

                // Loop through all symbol layers and lock the color.
                foreach (SymbolLayer lyr in faceSymbol.SymbolLayers)
                {
                    // Changing the color of the symbol will not affect this layer.
                    lyr.IsColorLocked = true;
                }

                // Unlock the color for the base (first) layer. Changing the symbol color will change this layer's color.
                faceSymbol.SymbolLayers.First().IsColorLocked = false;

                // Set the symbol color from the combo box.
                if (FaceColorComboBox.SelectedItem != null)
                {
                    faceSymbol.Color = (Color)FaceColorComboBox.SelectedItem;
                }

                // Set the symbol size from the slider.
                faceSymbol.Size = SizeSlider.Value;
            }
            catch (Exception ex)
            {
                // Report the exception.
                MessageDialog dialog = new MessageDialog("Error creating the symbol: " + ex.Message);
                await dialog.ShowAsync();
            }

            // Return the multilayer point symbol.
            return faceSymbol;
        }

        private async Task UpdateSymbolPreview(Symbol symbolToShow)
        {
            if (symbolToShow == null) { return; }

            // Create a swatch from the symbol with a white background.
            RuntimeImage swatch = await symbolToShow.CreateSwatchAsync(80, 80, 96, Color.White);

            // Convert the swatch to an image source and show it in the Image control.
            ImageSource symbolImage = await swatch.ToImageSourceAsync();
            SymbolPreviewImage.Source = symbolImage;
        }

        // A handler for the click event on the clear graphics button.
        private void ClearGraphicsClick(object sender, RoutedEventArgs e)
        {
            // Clear all graphics from the first (only) graphics overlay in the map view.
            MyMapView.GraphicsOverlays.FirstOrDefault()?.Graphics.Clear();
        }
    }

    // A class for storing information about a symbol layer.
    public class SymbolLayerInfo
    {
        // An image source for a preview image of the symbol.
        public ImageSource ImageSrc { get; private set; }

        // The name of the symbol as it appears in the mobile style.
        public string Name { get; private set; }

        // A key that uniquely identifies the symbol in the style.
        public string Key { get; private set; }

        // Take all symbol info in the constructor.
        public SymbolLayerInfo(string name, ImageSource source, string key)
        {
            Name = name;
            ImageSrc = source;
            Key = key;
        }
    }

    // A class that converts a System.Drawing.Color object to a solid brush for setting background color for UI controls. 
    public class ColorToSolidBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            // Get the input value as a System.Drawing.Color.
            Color inColor = (Color)value;

            // Create a Windows.UI.Color from the System.Drawing.Color.
            Windows.UI.Color outColor = Windows.UI.Color.FromArgb(inColor.A, inColor.R, inColor.G, inColor.B);

            // Create a solid color brush using the color and return it.
            SolidColorBrush brush = new SolidColorBrush(outColor);
            return brush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}