Home > Net >  Countdown timers and images in Xamarin.Forms
Countdown timers and images in Xamarin.Forms

Time:02-11

I have a question about countdown timers and images in Xamarin.Forms I am hoping you can help me with.

I have set up a timer which counts down from 5 to 0 and repeats at 0 with new text and image through a series. The issue I am having is that the image blinks with each count down number. I understand that the screen is refreshing but is there a way to stop the refresh of the image each second?

Device.StartTimer(TimeSpan.FromSeconds(1), () =>
        {
            _countSeconds--;

            if (_countSeconds == 0)
            {
                ndx  ;
                _countSeconds = 5;
            };

            if (ndx < DailyT.Count && ndx >= 0)
            {
                BindingContext = DailyT[ndx];
                MyImage.Source = ImageSource.FromResource(DailyT[ndx].ImageA, typeof(BGLongsword).GetTypeInfo().Assembly);

                //ISSUE: Image blinks each count down second
            }
            else
            {
                return false;
            }

            CountLabel.Text = _countSeconds.ToString();

            return true;
        });

CodePudding user response:

you only need to update the image when ndx changes

        if (_countSeconds == 0)
        {
            ndx  ;
            _countSeconds = 5;

            if (ndx < DailyT.Count && ndx >= 0)
            {
               MyImage.Source = ImageSource.FromResource(DailyT[ndx].ImageA, typeof(BGLongsword).GetTypeInfo().Assembly);
            }
            else
            {
               return false;
            }
        };

        
  • Related