Read symbols from mobile style

View inMAUIUWPWPFWinUIView on GitHub

Combine multiple symbols from a mobile style file into a single symbol.

Image of read symbols from mobile style

Use case

You may choose to display individual elements of a dataset like a water infrastructure network (such as valves, nodes, or endpoints) with the same basic shape, but wish to modify characteristics of elements according to some technical specifications. Multilayer symbols lets you add or remove components or modify the colors to create advanced symbol styles.

How to use the sample

Select a symbol and a color from each of the category lists to create an emoji. A preview of the symbol is updated as selections are made. The size of the symbol can be set using the slider. Tap the map to create a point graphic using the customized emoji symbol, and tap "Reset" 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

Emoji mobile style

About the data

The mobile style file used in this sample was created using ArcGIS Pro, and is hosted on ArcGIS Online. It contains symbol layers that can be combined to create emojis.

Additional information

While each of 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 Maps SDK for .NET can read symbols from a mobile style, and you can modify and combine them as needed in your app.

Tags

advanced symbology, mobile style, multilayer, stylx

Sample Code

SymbolsFromMobileStyle.xaml.csSymbolsFromMobileStyle.xaml.csSymbolsFromMobileStyle.xaml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2022 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 ArcGIS.Samples.Managers;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Maui;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using Color = System.Drawing.Color;
using RuntimeImageExtensions = Esri.ArcGISRuntime.Maui.RuntimeImageExtensions;

namespace ArcGIS.Samples.SymbolsFromMobileStyle
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Read symbols from mobile style",
        category: "Symbology",
        description: "Combine multiple symbols from a mobile style file into a single symbol.",
        instructions: "Select a symbol and a color from each of the category lists to create an emoji. A preview of the symbol is updated as selections are made. The size of the symbol can be set using the slider. Tap the map to create a point graphic using the customized emoji symbol, and tap \"Reset\" to clear all graphics from the display.",
        tags: new[] { "advanced symbology", "mobile style", "multilayer", "stylx" })]
    [ArcGIS.Samples.Shared.Attributes.OfflineData("1bd036f221f54a99abc9e46ff3511cbf")]
    public partial class SymbolsFromMobileStyle : ContentPage
    {
        // 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(BasemapStyle.ArcGISTopographic);
            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
            };
            ColorListView.ItemsSource = faceColors;

            // 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 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("", null, "") };
                List<SymbolLayerInfo> mouthSymbolInfos = new List<SymbolLayerInfo> { new SymbolLayerInfo("", null, "") };
                List<SymbolLayerInfo> hatSymbolInfos = new List<SymbolLayerInfo>() { new SymbolLayerInfo("", null, "") };

                // 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 = await result.GetSymbolAsync() as MultilayerPointSymbol;

                    // Create a swatch image from the symbol.
                    RuntimeImage swatch = await multiLayerSym.CreateSwatchAsync(30, 30, 96, Color.Transparent);

                    // Create an image source from the swatch.
                    Stream imageBuffer = await swatch.GetEncodedBufferAsync();
                    byte[] imageData = new byte[imageBuffer.Length];
                    imageBuffer.Read(imageData, 0, imageData.Length);
                    ImageSource symbolImage = ImageSource.FromStream(() => new MemoryStream(imageData));

                    // 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.
                EyesListView.ItemsSource = eyeSymbolInfos;
                MouthListView.ItemsSource = mouthSymbolInfos;
                HatListView.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.
                await DisplayAlert("Error reading style", ex.Message, "OK");
            }
        }

        // Handler for the tapped event on the map view.
        private async void GeoViewTapped(object sender, GeoViewInputEventArgs e)
        {
            try
            {
                // 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);
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
            }
        }

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

                // Call a function to update the symbol preview.
                await UpdateSymbolPreview(faceSymbol);
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
            }
        }

        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)EyesListView.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)MouthListView.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)HatListView.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 (ColorListView.SelectedItem != null)
                {
                    faceSymbol.Color = (Color)ColorListView.SelectedItem;
                }

                // Set the symbol size from the slider.
                faceSymbol.Size = SizeSlider.Value;
            }
            catch (Exception ex)
            {
                // Report the exception.
                await DisplayAlert("Error creating symbol", ex.Message, "OK");
            }

            // 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.Transparent);

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

        // A handler for the click event on the clear graphics button.
        private void ClearGraphicsButtonClicked(object sender, EventArgs 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;
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.