Configure subnetwork trace

View inAndroidFormsUWPWPFWinUIiOSView on GitHub

Get a server-defined trace configuration for a given tier and modify its traversability scope, add new condition barriers and control what is included in the subnetwork trace result.

Image of configure subnetwork trace

Use case

While some traces are built from an ad-hoc group of parameters, many are based on a variation of the trace configuration taken from the subnetwork definition. For example, an electrical trace will be based on the trace configuration of the subnetwork, but may add additional clauses to constrain the trace along a single phase. Similarly, a trace in a gas or electric design application may include features with a status of "In Design" that are normally excluded from trace results.

How to use the sample

The sample loads with a server-defined trace configuration from a tier. Check or uncheck which options to include in the trace - such as containers or barriers. Use the selection boxes to define a new condition network attribute comparison, and then use 'Add' to add the it to the trace configuration. Tap 'Trace' to run a subnetwork trace with this modified configuration from a default starting location.

Example barrier conditions for the default dataset:

  • 'Transformer Load' Equal '15'
  • 'Phases Current' DoesNotIncludeTheValues 'A'
  • 'Generation KW' LessThan '50'

How it works

  1. Create and load a UtilityNetwork with a feature service URL, then get an asset type and a tier by their names.
  2. Populate the choice list for the comparison source with the non-system defined Definition.NetworkAttributes. Populate the choice list for the comparison operator with the enum values from UtilityAttributeComparisonOperator.
  3. Create a UtilityElement from this asset type to use as the starting location for the trace.
  4. Update the selected barrier expression and the checked options in the UI using this tier's TraceConfiguration.
  5. When 'Network Attribute' is selected, if its Domain is a CodedValueDomain, populate the choice list for the comparison value with its CodedValues. Otherwise, display a free-form textbox for entering an attribute value.
  6. When 'Add' is clicked, create a new UtilityNetworkAttributeComparison using the selected comparison source, operator, and selected or typed value. Use the selected source's NetworkAttribute.DataType to convert the comparison value to the correct data type.
  7. If the Traversability's list of Barriers is not empty, create a UtilityTraceOrCondition with the existing Barriers and the new comparison from Step 6.
  8. When 'Trace' is clicked, create UtilityTraceParameters passing in UtilityTraceType.Subnetwork and the default starting location. Set its TraceConfiguration with the modified options, selections, and expression; then run a UtilityNetwork.TraceAsync.
  9. When Reset is clicked, set the trace configurations expression back to its original value.
  10. Display the count of returned UtilityElementTraceResult.Elements.

Relevant API

  • CodedValueDomain
  • UtilityAssetType
  • UtilityAttributeComparisonOperator
  • UtilityCategory
  • UtilityCategoryComparison
  • UtilityCategoryComparisonOperator
  • UtilityDomainNetwork
  • UtilityElement
  • UtilityElementTraceResult
  • UtilityNetwork
  • UtilityNetworkAttribute
  • UtilityNetworkAttributeComparison
  • UtilityNetworkDefinition
  • UtilityTerminal
  • UtilityTier
  • UtilityTraceAndCondition
  • UtilityTraceConfiguration
  • UtilityTraceOrCondition
  • UtilityTraceParameters
  • UtilityTraceResult
  • UtilityTraceType
  • UtilityTraversability

About the data

The Naperville electrical network feature service, hosted on ArcGIS Online, contains a utility network used to run the subnetwork-based trace shown in this sample.

Additional information

Using utility network on ArcGIS Enterprise 10.8 requires an ArcGIS Enterprise member account licensed with the Utility Network user type extension. Please refer to the utility network services documentation.

Tags

category comparison, condition barriers, network analysis, network attribute comparison, subnetwork trace, trace configuration, traversability, utility network, validate consistency

Sample Code

ConfigureSubnetworkTrace.cs
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Copyright 2021 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;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Security;
using Esri.ArcGISRuntime.UtilityNetworks;
using Foundation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UIKit;

namespace ArcGISRuntimeXamarin.Samples.ConfigureSubnetworkTrace
{
    [Register("ConfigureSubnetworkTrace")]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Configure subnetwork trace",
        category: "Utility network",
        description: "Get a server-defined trace configuration for a given tier and modify its traversability scope, add new condition barriers and control what is included in the subnetwork trace result.",
        instructions: "The sample loads with a server-defined trace configuration from a tier. Check or uncheck which options to include in the trace - such as containers or barriers. Use the selection boxes to define a new condition network attribute comparison, and then use 'Add' to add the it to the trace configuration. Tap 'Trace' to run a subnetwork trace with this modified configuration from a default starting location.",
        tags: new[] { "category comparison", "condition barriers", "network analysis", "network attribute comparison", "subnetwork trace", "trace configuration", "traversability", "utility network", "validate consistency" })]
    public class ConfigureSubnetworkTrace : UIViewController
    {
        // References to controls and labels in the UI.
        private UISwitch _barrierSwitch;
        private UISwitch _containerSwitch;
        private UILabel _expressionLabel;
        private UIButton _attributeButton;
        private UIButton _comparisonButton;
        private UIButton _valueButton;
        private UITextField _valueTextEntry;
        private UIButton _addButton;
        private UIButton _traceButton;
        private UIButton _resetButton;

        // Feature service for an electric utility network in Naperville, Illinois.
        private const string FeatureServiceUrl = "https://sampleserver7.arcgisonline.com/server/rest/services/UtilityNetwork/NapervilleElectric/FeatureServer";
        private UtilityNetwork _utilityNetwork;

        // For creating the default starting location.
        private const string DeviceTableName = "Electric Distribution Device";
        private const string AssetGroupName = "Circuit Breaker";
        private const string AssetTypeName = "Three Phase";
        private const string GlobalId = "{1CAF7740-0BF4-4113-8DB2-654E18800028}";

        // For creating the default trace configuration.
        private const string DomainNetworkName = "ElectricDistribution";
        private const string TierName = "Medium Voltage Radial";

        // Utility element to start the trace from.
        private UtilityElement _startingLocation;

        // Holding the initial conditional expression.
        private UtilityTraceConditionalExpression _initialExpression;

        // The trace configuration.
        private UtilityTraceConfiguration _configuration;

        // The source tier of the utility network.
        private UtilityTier _sourceTier;

        // The currently selected values for the barrier expression.
        private UtilityNetworkAttribute _selectedAttribute;
        private UtilityAttributeComparisonOperator _selectedComparison;
        private object _selectedValue;

        public ConfigureSubnetworkTrace()
        {
            Title = "Configure subnetwork trace";
        }

        private async void Initialize()
        {
            // As of ArcGIS Enterprise 10.8.1, using utility network functionality requires a licensed user. The following login for the sample server is licensed to perform utility network operations.
            AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(async (info) =>
            {
                try
                {
                    // WARNING: Never hardcode login information in a production application. This is done solely for the sake of the sample.
                    string sampleServer7User = "viewer01";
                    string sampleServer7Pass = "I68VGU^nMurF";
                    return await AuthenticationManager.Current.GenerateCredentialAsync(info.ServiceUri, sampleServer7User, sampleServer7Pass);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    return null;
                }
            });

            try
            {
                // Disable interaction until the data is loaded.
                View.UserInteractionEnabled = false;

                // Create and load the utility network.
                _utilityNetwork = await UtilityNetwork.CreateAsync(new Uri(FeatureServiceUrl));

                // Create a default starting location.
                UtilityNetworkSource networkSource = _utilityNetwork.Definition.GetNetworkSource(DeviceTableName);
                UtilityAssetGroup assetGroup = networkSource.GetAssetGroup(AssetGroupName);
                UtilityAssetType assetType = assetGroup.GetAssetType(AssetTypeName);
                Guid globalId = Guid.Parse(GlobalId);
                _startingLocation = _utilityNetwork.CreateElement(assetType, globalId);

                // Set the terminal for this location. (For our case, we use the 'Load' terminal.)
                _startingLocation.Terminal = _startingLocation.AssetType.TerminalConfiguration?.Terminals.FirstOrDefault(term => term.Name == "Load");

                // Get a default trace configuration from a tier to update the UI.
                UtilityDomainNetwork domainNetwork = _utilityNetwork.Definition.GetDomainNetwork(DomainNetworkName);
                _sourceTier = domainNetwork.GetTier(TierName);

                // Set the trace configuration.
                _configuration = _sourceTier.GetDefaultTraceConfiguration();

                // Set the default expression (if provided).
                if (_sourceTier.GetDefaultTraceConfiguration().Traversability.Barriers is UtilityTraceConditionalExpression expression)
                {
                    _expressionLabel.Text = ExpressionToString(expression);
                    _initialExpression = expression;
                }

                // Set the traversability scope.
                _sourceTier.GetDefaultTraceConfiguration().Traversability.Scope = UtilityTraversabilityScope.Junctions;
            }
            catch (Exception ex)
            {
                new UIAlertView(ex.GetType().Name, ex.Message, (IUIAlertViewDelegate)null, "Error loading network", null).Show();
            }
            finally
            {
                View.UserInteractionEnabled = true;
            }
        }

        private void BarrierChanged(object sender, EventArgs e)
        {
            _sourceTier.GetDefaultTraceConfiguration().IncludeBarriers = _barrierSwitch.On;
        }

        private void ContainerChanged(object sender, EventArgs e)
        {
            _sourceTier.GetDefaultTraceConfiguration().IncludeContainers = _containerSwitch.On;
        }

        private void AttributeClick(object sender, EventArgs e)
        {
            // Create a prompt for selecting the attribute of the barrier expression.
            UIAlertController prompt = UIAlertController.Create(null, "Choose the attribute.", UIAlertControllerStyle.Alert);
            foreach (UtilityNetworkAttribute attribute in _utilityNetwork.Definition.NetworkAttributes.Where(i => i.IsSystemDefined == false))
            {
                UIAlertAction action = UIAlertAction.Create(attribute.Name, UIAlertActionStyle.Default, (s) => AttributeSelected(attribute));
                prompt.AddAction(action);
            }
            prompt.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null));
            PresentViewController(prompt, true, null);
        }

        private void AttributeSelected(UtilityNetworkAttribute attribute)
        {
            _attributeButton.SetTitle(attribute.Name, UIControlState.Normal);
            _selectedAttribute = attribute;

            // Reset the value, this prevents invalid values being used to create expressions.
            _valueButton.SetTitle("Value", UIControlState.Normal);
            _selectedValue = null;
        }

        private void ComparisonClick(object sender, EventArgs e)
        {
            // Create a prompt for selecting the comparison operator.
            UIAlertController prompt = UIAlertController.Create(null, "Choose the comparison.", UIAlertControllerStyle.Alert);
            foreach (UtilityAttributeComparisonOperator op in Enum.GetValues(typeof(UtilityAttributeComparisonOperator)))
            {
                UIAlertAction action = UIAlertAction.Create(op.ToString(), UIAlertActionStyle.Default, (s) => ComparisonSelected(op));
                prompt.AddAction(action);
            }
            prompt.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null));
            PresentViewController(prompt, true, null);
        }

        private void ComparisonSelected(UtilityAttributeComparisonOperator op)
        {
            _comparisonButton.SetTitle(op.ToString(), UIControlState.Normal);
            _selectedComparison = op;
        }

        private void ValueClick(object sender, EventArgs e)
        {
            // Verify that an attribute has been selected.
            if (_selectedAttribute != null)
            {
                UIAlertController prompt = null;
                if (_selectedAttribute.Domain is CodedValueDomain domain)
                {
                    // Add every coded value as an action on the prompt.
                    prompt = UIAlertController.Create(null, "Choose the value.", UIAlertControllerStyle.Alert);
                    foreach (CodedValue value in domain.CodedValues)
                    {
                        UIAlertAction action = UIAlertAction.Create(value.Name, UIAlertActionStyle.Default, (s) => ValueSelected(value));
                        prompt.AddAction(action);
                    }
                }
                else
                {
                    // Create an alert for the user to enter the value using the keyboard.
                    prompt = UIAlertController.Create(null, "Enter the value", UIAlertControllerStyle.Alert);
                    prompt.AddTextField(obj =>
                    {
                        _valueTextEntry = obj;
                        obj.Text = "";
                        obj.KeyboardType = UIKeyboardType.NumberPad;
                    });
                    prompt.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, ValueEntered));
                }
                prompt.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null));
                PresentViewController(prompt, true, null);
            }
        }

        private void ValueEntered(UIAlertAction action)
        {
            _valueButton.SetTitle(_valueTextEntry.Text, UIControlState.Normal);
            _selectedValue = _valueTextEntry.Text;
        }

        private void ValueSelected(CodedValue val)
        {
            _valueButton.SetTitle(val.Name, UIControlState.Normal);
            _selectedValue = val;
        }

        private void AddClick(object sender, EventArgs e)
        {
            try
            {
                if (_configuration == null)
                {
                    _configuration = new UtilityTraceConfiguration();
                }
                if (_configuration.Traversability == null)
                {
                    _configuration.Traversability = new UtilityTraversability();
                }

                // NOTE: You may also create a UtilityCategoryComparison with UtilityNetworkDefinition.Categories and UtilityCategoryComparisonOperator.
                if (_selectedAttribute != null)
                {
                    object selectedValue;

                    // If the value is a coded value.
                    if (_selectedAttribute.Domain is CodedValueDomain && _selectedValue is CodedValue codedValue)
                    {
                        selectedValue = ConvertToDataType(codedValue.Code, _selectedAttribute.DataType);
                    }
                    // If the value is free entry.
                    else
                    {
                        selectedValue = ConvertToDataType(_selectedValue.ToString().Trim(), _selectedAttribute.DataType);
                    }
                    // NOTE: You may also create a UtilityNetworkAttributeComparison with another NetworkAttribute.
                    UtilityTraceConditionalExpression expression = new UtilityNetworkAttributeComparison(_selectedAttribute, _selectedComparison, selectedValue);
                    if (_configuration.Traversability.Barriers is UtilityTraceConditionalExpression otherExpression)
                    {
                        // NOTE: You may also combine expressions with UtilityTraceAndCondition
                        expression = new UtilityTraceOrCondition(otherExpression, expression);
                    }
                    _configuration.Traversability.Barriers = expression;
                    _expressionLabel.Text = ExpressionToString(expression);
                }
            }
            catch (Exception ex)
            {
                new UIAlertView(ex.GetType().Name, ex.Message, (IUIAlertViewDelegate)null, "Error adding barrier", null).Show();
            }
        }

        private async void TraceClick(object sender, EventArgs e)
        {
            if (_utilityNetwork == null || _startingLocation == null)
            {
                return;
            }
            try
            {
                // Create utility trace parameters for the starting location.
                UtilityTraceParameters parameters = new UtilityTraceParameters(UtilityTraceType.Subnetwork, new[] { _startingLocation });
                parameters.TraceConfiguration = _configuration;

                // Trace the utility network.
                IEnumerable<UtilityTraceResult> results = await _utilityNetwork.TraceAsync(parameters);

                // Get the first result.
                UtilityElementTraceResult elementResult = results?.FirstOrDefault() as UtilityElementTraceResult;

                // Display the number of elements found by the trace.
                new UIAlertView("Trace Result", $"`{elementResult?.Elements?.Count ?? 0}` elements found.", (IUIAlertViewDelegate)null, "OK", null).Show();
            }
            catch (Exception ex)
            {
                new UIAlertView(ex.GetType().Name, ex.Message, (IUIAlertViewDelegate)null, "OK", null).Show();
            }
        }

        private void ResetClick(object sender, EventArgs e)
        {
            // Reset the barrier condition to the initial value.
            UtilityTraceConfiguration traceConfiguration = _configuration;
            traceConfiguration.Traversability.Barriers = _initialExpression;
            _expressionLabel.Text = ExpressionToString(_initialExpression);
        }

        private object ConvertToDataType(object otherValue, UtilityNetworkAttributeDataType dataType)
        {
            switch (dataType)
            {
                case UtilityNetworkAttributeDataType.Boolean:
                    return Convert.ToBoolean(otherValue);

                case UtilityNetworkAttributeDataType.Double:
                    return Convert.ToDouble(otherValue);

                case UtilityNetworkAttributeDataType.Float:
                    return Convert.ToSingle(otherValue);

                case UtilityNetworkAttributeDataType.Integer:
                    return Convert.ToInt32(otherValue);
            }
            throw new NotSupportedException();
        }

        private string ExpressionToString(UtilityTraceConditionalExpression expression)
        {
            if (expression is UtilityCategoryComparison categoryComparison)
            {
                return $"`{categoryComparison.Category.Name}` {categoryComparison.ComparisonOperator}";
            }
            else if (expression is UtilityNetworkAttributeComparison attributeComparison)
            {
                // Check if attribute domain is a coded value domain.
                if (attributeComparison.NetworkAttribute.Domain is CodedValueDomain domain)
                {
                    // Get the coded value using the the attribute comparison value and attribute data type.
                    UtilityNetworkAttributeDataType dataType = attributeComparison.NetworkAttribute.DataType;
                    object attributeValue = ConvertToDataType(attributeComparison.Value, attributeComparison.NetworkAttribute.DataType);
                    CodedValue codedValue = domain.CodedValues.FirstOrDefault(value => ConvertToDataType(value.Code, dataType).Equals(attributeValue));
                    return $"`{attributeComparison.NetworkAttribute.Name}` {attributeComparison.ComparisonOperator} `{codedValue?.Name}`";
                }
                else
                {
                    return $"`{attributeComparison.NetworkAttribute.Name}` {attributeComparison.ComparisonOperator} `{attributeComparison.OtherNetworkAttribute?.Name ?? attributeComparison.Value}`";
                }
            }
            else if (expression is UtilityTraceAndCondition andCondition)
            {
                return $"({ExpressionToString(andCondition.LeftExpression)}) AND\n ({ExpressionToString(andCondition.RightExpression)})";
            }
            else if (expression is UtilityTraceOrCondition orCondition)
            {
                return $"({ExpressionToString(orCondition.LeftExpression)}) OR\n ({ExpressionToString(orCondition.RightExpression)})";
            }
            else
            {
                return null;
            }
        }

        public override void LoadView()
        {
            // Create the views.
            View = new UIView() { BackgroundColor = ApplicationTheme.BackgroundColor };

            UIScrollView scrollView = new UIScrollView();
            scrollView.TranslatesAutoresizingMaskIntoConstraints = false;

            View.AddSubviews(scrollView);

            NSLayoutConstraint.ActivateConstraints(new[]
            {
                scrollView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
                scrollView.LeadingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.LeadingAnchor),
                scrollView.TrailingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TrailingAnchor),
                scrollView.BottomAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.BottomAnchor),
            });

            UIStackView buttonContainer = new UIStackView();
            buttonContainer.Axis = UILayoutConstraintAxis.Vertical;
            buttonContainer.TranslatesAutoresizingMaskIntoConstraints = false;
            buttonContainer.Distribution = UIStackViewDistribution.Fill;
            buttonContainer.Alignment = UIStackViewAlignment.Top;
            buttonContainer.Spacing = 5;
            buttonContainer.LayoutMarginsRelativeArrangement = true;
            buttonContainer.DirectionalLayoutMargins = new NSDirectionalEdgeInsets(10, 10, 10, 0);

            UILabel barrierLabel = new UILabel() { Text = "Include barriers   ", TranslatesAutoresizingMaskIntoConstraints = false };
            _barrierSwitch = new UISwitch() { TranslatesAutoresizingMaskIntoConstraints = false, On = true };
            buttonContainer.AddArrangedSubview(GetRowStackView(new UIView[] { barrierLabel, _barrierSwitch }));

            UILabel containerLabel = new UILabel() { Text = "Include containers", TranslatesAutoresizingMaskIntoConstraints = false };
            _containerSwitch = new UISwitch() { TranslatesAutoresizingMaskIntoConstraints = false, On = true };
            buttonContainer.AddArrangedSubview(GetRowStackView(new UIView[] { containerLabel, _containerSwitch }));

            UILabel helpLabel = new UILabel() { Text = "Example barrier condition for this data: 'Transformer Load' Equal '15'", TranslatesAutoresizingMaskIntoConstraints = false, Lines = 0 };
            buttonContainer.AddArrangedSubview(helpLabel);

            UILabel conditionTitleLabel = new UILabel() { Text = "Barrier Condition:", TranslatesAutoresizingMaskIntoConstraints = false, Lines = 0, MinimumFontSize = (nfloat)(helpLabel.MinimumFontSize * 1.5) };
            buttonContainer.AddArrangedSubview(conditionTitleLabel);

            _attributeButton = new UIButton() { TranslatesAutoresizingMaskIntoConstraints = false };
            _attributeButton.SetTitle("Attribute", UIControlState.Normal);
            _attributeButton.SetTitleColor(View.TintColor, UIControlState.Normal);
            _comparisonButton = new UIButton() { TranslatesAutoresizingMaskIntoConstraints = false };
            _comparisonButton.SetTitle("Comparison", UIControlState.Normal);
            _comparisonButton.SetTitleColor(View.TintColor, UIControlState.Normal);
            _valueButton = new UIButton() { TranslatesAutoresizingMaskIntoConstraints = false };
            _valueButton.SetTitle("Value", UIControlState.Normal);
            _valueButton.SetTitleColor(View.TintColor, UIControlState.Normal);
            buttonContainer.AddArrangedSubview(GetRowStackView(new UIView[] { _attributeButton, _comparisonButton, _valueButton }));

            _addButton = new UIButton() { TranslatesAutoresizingMaskIntoConstraints = false };
            _addButton.SetTitle("Add barrier condition", UIControlState.Normal);
            _addButton.SetTitleColor(View.TintColor, UIControlState.Normal);
            buttonContainer.AddArrangedSubview(GetRowStackView(new UIView[] { _addButton }));

            _expressionLabel = new UILabel() { TranslatesAutoresizingMaskIntoConstraints = false, Text = "", Lines = 0 };
            buttonContainer.AddArrangedSubview(_expressionLabel);

            _traceButton = new UIButton() { TranslatesAutoresizingMaskIntoConstraints = false };
            _traceButton.SetTitle("Trace", UIControlState.Normal);
            _traceButton.SetTitleColor(View.TintColor, UIControlState.Normal);

            _resetButton = new UIButton() { TranslatesAutoresizingMaskIntoConstraints = false };
            _resetButton.SetTitle("Reset", UIControlState.Normal);
            _resetButton.SetTitleColor(View.TintColor, UIControlState.Normal);
            buttonContainer.AddArrangedSubview(GetRowStackView(new UIView[] { _traceButton, _resetButton }));

            scrollView.AddSubview(buttonContainer);

            NSLayoutConstraint.ActivateConstraints(new[]
            {
                buttonContainer.TopAnchor.ConstraintEqualTo(scrollView.TopAnchor),
                buttonContainer.LeadingAnchor.ConstraintEqualTo(scrollView.LeadingAnchor),
                buttonContainer.TrailingAnchor.ConstraintEqualTo(scrollView.TrailingAnchor),
                buttonContainer.BottomAnchor.ConstraintEqualTo(scrollView.BottomAnchor),
                buttonContainer.WidthAnchor.ConstraintEqualTo(scrollView.WidthAnchor),
            });
        }

        private UIStackView GetRowStackView(UIView[] views)
        {
            UIStackView row = new UIStackView(views);
            row.TranslatesAutoresizingMaskIntoConstraints = false;
            row.Spacing = 8;
            row.Axis = UILayoutConstraintAxis.Horizontal;
            row.Distribution = UIStackViewDistribution.EqualCentering;
            row.WidthAnchor.ConstraintEqualTo(350).Active = true;
            return row;
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            Initialize();
        }

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            // Subscribe to events.
            _attributeButton.TouchUpInside += AttributeClick;
            _comparisonButton.TouchUpInside += ComparisonClick;
            _valueButton.TouchUpInside += ValueClick;
            _addButton.TouchUpInside += AddClick;
            _traceButton.TouchUpInside += TraceClick;
            _resetButton.TouchUpInside += ResetClick;
            _barrierSwitch.ValueChanged += BarrierChanged;
            _containerSwitch.ValueChanged += ContainerChanged;
        }

        public override void ViewDidDisappear(bool animated)
        {
            base.ViewDidDisappear(animated);

            // Unsubscribe from events, per best practice.
            _attributeButton.TouchUpInside -= AttributeClick;
            _comparisonButton.TouchUpInside -= ComparisonClick;
            _valueButton.TouchUpInside -= ValueClick;
            _addButton.TouchUpInside -= AddClick;
            _traceButton.TouchUpInside -= TraceClick;
            _resetButton.TouchUpInside -= ResetClick;
            _barrierSwitch.ValueChanged -= BarrierChanged;
            _containerSwitch.ValueChanged -= ContainerChanged;
        }
    }
}

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