Functionality5.xaml.cs 2.47 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
using System;
using System.Reflection;
using SampleApp.DependencyServices;
using Xamarin.Forms;

namespace SampleApp.Views
{
    public partial class Functionality5 : ContentPage
    {


        //label
        Label  label= new Label { Text = "Does the Bell's Image Have the Focus?", HorizontalOptions = LayoutOptions.CenterAndExpand };
        Label labelYN = new Label { Text = "YES/ NO", HorizontalOptions = LayoutOptions.CenterAndExpand };
        Label infoLabel = new Label { Text = "i: This view updates every 2 seconds", HorizontalOptions = LayoutOptions.CenterAndExpand, Margin = new Thickness(0, 200) };

        IIsFocused dependency;

        bool onView = true;

        public Functionality5()
        {
            InitializeComponent();

            dependency = DependencyService.Get<IIsFocused>();


            background.Children.Add(label);
            background.Children.Add(labelYN);
            background.Children.Add(infoLabel);

            

            AutomationProperties.SetIsInAccessibleTree(label, true);
            AutomationProperties.SetIsInAccessibleTree(labelYN, true);
            AutomationProperties.SetIsInAccessibleTree(infoLabel, true);
            AutomationProperties.SetIsInAccessibleTree(bellImage, true);
            AutomationProperties.SetHelpText(bellImage, "Bell Image");


            // timer che si ripete ogni 2 secondi
            Device.StartTimer(TimeSpan.FromSeconds(2.0), () =>
            {

                if (onView)
                {
                    Console.WriteLine("TIMER FIRED");
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        if (dependency.isFocused(bellImage))
                        {
                            Console.WriteLine("is focused");
                            labelYN.TextColor = Color.Green;
                            labelYN.Text = "YES";
                        }
                        else
                        {
                            Console.WriteLine("is NOT focused");
                            labelYN.TextColor = Color.Red;
                            labelYN.Text = "NO";
                        }
                    });
                    return true; // timer runs again
                }
                return false; //timer stops
            });
        }


        //stop the timer when view changes
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            onView = false;
        }

    }

}