Forum Cross Platform with Xamarin

Use timer to change text of a label

I am looking for something to change the text of a label with a timer.

 Device.StartTimer(TimeSpan.FromSeconds(10), () =>
                {

                Show.Text = "Joejoe";
                    return true; // return true to repeat counting, false to stop timer
            });

But am looking for some sort of loop that the text is changing every 10 seconds.

The text has about 5 diverent name's

For example

Joejoe
Bingo
Birthday
Party
Meeting

Do i need to start the timer 5 time's , no that is not working i think and when the last word is showing
it has to start over again with the first word.

What to add or change ?

Tagged:

Best Answers

Answers

  • BorisopritBorisoprit Member ✭✭✭

    Hello jezh,

    Thanks for the reply

    Got this error

    Cannot apply index with [] to an expression to type 'object'
    
    
    public partial class MainPage : ContentPage
        {
            private int number;
            private object arr;
    
            public MainPage()
            {
                InitializeComponent();
    
                string[] arr;
                int number = 0;
                arr = new string[5] { "JoeJoe", "Bingo", "Birthday", "Party", "Meeting" };
    
                Device.StartTimer(TimeSpan.FromSeconds(10), () => OnTimerTick());
    
    
    
            }
    
            bool OnTimerTick()
            {
    
                Device.BeginInvokeOnMainThread(async () =>
                {
                    if (0 == number)
                    {
                        Show.Text = arr[0];
                        number++;
                    }
                    else if (1 == number)
                    {
                        Show.Text = arr[1];
                        number++;
                    }
                    else if (2 == number)
                    {
                        Show.Text = arr[2];
                        number++;
                    }
                    else if (3 == number)
                    {
                        Show.Text = arr[3];
                        number++;
                    }
                    else if (4 == number)
                    {
                        Show.Text = arr[4];
                        number = 0;
                    }
                });
                return true;
            }
    
        }
    
    
  • jezhjezh Member, Xamarin Team Xamurai
    edited February 17

    Hi ,friend, just change the following code:

      private object arr;
    

    to

      private   string[] arr ;
    

    Note: While the type of arr; you definded private object arr; is object, but in your code as follows you used it as a array.

     arr = new string[5] { "JoeJoe", "Bingo", "Birthday", "Party", "Meeting" };
    
  • JohnHardmanJohnHardman GBUniversity admin

    @Borisoprit said:

    public partial class MainPage : ContentPage
        {
            private int number;
            private object arr;
    
            public MainPage()
            {
                InitializeComponent();
    
                string[] arr;
    

    The problem is that you have added string[] arr; inside the MainPage constructor, whilst also retaining private object arr; at class level. What you should have done is to replace the latter with the former.

  • JohnHardmanJohnHardman GBUniversity admin

    @Borisoprit - Dont forget to Like and/or mark as Accepted Answer any posts that help and/or answer your question.

  • BorisopritBorisoprit Member ✭✭✭

    I got en error

    Show.Text = arr[0];
    
    System.NullReferenceException
      Message=Object reference not set to an instance of an object
    
    
  • BorisopritBorisoprit Member ✭✭✭
    MainPage.arr' is never assigned to, and will always have its default value null
    
    
  • JohnHardmanJohnHardman GBUniversity admin

    @Borisoprit said:

    MainPage.arr' is never assigned to, and will always have its default value null
    
    

    You must be missing the last line from https://forums.xamarin.com/discussion/comment/403237/#Comment_403237

  • BorisopritBorisoprit Member ✭✭✭

    I started a new project and it is working now ,

    Thanks jezh and JohnHardman .

Sign In or Register to comment.