Functionality18.xaml.cs 3.21 KB
Newer Older
Mattia's avatar
Mattia committed
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
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace SampleApp.Views
{
    public partial class Functionality18 : ContentPage
    {
        StackLayout aggregateElements;
        
        Label agg1, agg2, agg3;
        public Functionality18()
        {
            
                InitializeComponent();
                background.Padding = 10;

               


                //container for the aggregated views
                aggregateElements = new StackLayout
                {
                    BackgroundColor = Color.DarkMagenta,
                    HeightRequest = 150,
                    Padding = 10,
                };
                AutomationProperties.SetIsInAccessibleTree(aggregateElements, true);

                //the container that aggregates the views has to be accessible, setting his "AutomationProperties.IsInAccessibleTree" property to true
                if (Device.RuntimePlatform == Device.Android)
                {
                    AutomationProperties.SetIsInAccessibleTree(aggregateElements, true);
                }
                background.Children.Add(aggregateElements);

                //here I create the single views to be aggregated into one single element
                //all the "Label" in Xamarin have "AutomationProperties.IsInAccessibleTree" property setted to false by default
                agg1 = new Label
                {
                    Text = "View number 1",
                    BackgroundColor = Color.Orange,
                    HeightRequest = 40,
                };
                AutomationProperties.SetIsInAccessibleTree(agg1, false);
                agg2 = new Label
                {
                    Text = "View number 2",
                    BackgroundColor = Color.Orange,
                    HeightRequest = 40,
                };
                AutomationProperties.SetIsInAccessibleTree(agg2, false);
                agg3 = new Label
                {
                    Text = "This three different texts (views) have been grouped together, and will be seen as a single element.",
                    BackgroundColor = Color.Orange,
                    HeightRequest = 40,
                };
                AutomationProperties.SetIsInAccessibleTree(agg3, false);

                aggregateElements.Children.Add(agg1);
                aggregateElements.Children.Add(agg2);
                aggregateElements.Children.Add(agg3);

            }

            private void groupAccessibleItems(Layout container)
            {
                String desc = "";
                foreach (Label l in container.Children)
                {
                    Console.WriteLine($"PIPPO: id={l.Text}");
                    AutomationProperties.SetIsInAccessibleTree(l, false);
                    desc += l.Text;
                }
                Console.WriteLine($"PIPPO: totaldesc={desc}");
                AutomationProperties.SetIsInAccessibleTree(container, true);
                AutomationProperties.SetName(container, desc);
            }

        protected override void OnAppearing()
        {
            if (Device.RuntimePlatform == Device.iOS)
            {
                groupAccessibleItems(aggregateElements);
            }
        }
    
    }
}