Vladtn
Vladtn

Reputation: 2594

Xamarin Forms ProgressBar Stop

What is the best way to stop the progress bar animation in Xamarin Forms? Progress is started with:

animProgressBar = async () => { 
 ... 
 await progressBar.ProgressTo (0, 10000, Easing.Linear); 
 ... 
};

I would like to stop the animation and restart it. I have tried things like

progressBar.AbortAnimation("ProgressTo");

without success.

Upvotes: 3

Views: 4657

Answers (3)

stepkillah
stepkillah

Reputation: 1

You can also use progressBar.AbortAnimation("Progress");

as it's the actual name of animation in source code.

        public Task<bool> ProgressTo(double value, uint length, Easing easing)
        {
            var tcs = new TaskCompletionSource<bool>();

            this.Animate("Progress", d => Progress = d, Progress, value, length: length, easing: easing, finished: (d, finished) => tcs.SetResult(finished));

            return tcs.Task;
        }

https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/ProgressBar.cs#L37

Upvotes: 0

Mikalai Daronin
Mikalai Daronin

Reputation: 8716

I'm not sure that it is possible to cancel ProgressTo and I suppose that creating own animation is easiest solution:

new Button
{
    Text = "Kick progress bar",
    Command = new Command(() =>
    {
        if (progressBar.AnimationIsRunning("SetProgress"))
        {
            progressBar.AbortAnimation("SetProgress");
        }
        else
        {
            progressBar.Animate("SetProgress",(arg) => { progressBar.Progress = arg; }, 8*60, 8*1000, Easing.Linear);
        }
    })
}

Result:

enter image description here

Upvotes: 6

Yehor Hromadskyi
Yehor Hromadskyi

Reputation: 3388

I'd like to suggest you to set progressBar.Progress = 0 to reset all progress. I made quick example:

Device.StartTimer(System.TimeSpan.FromMilliseconds(200), () =>
        {
            myProgressBar.Progress += 0.01;
            if (myProgressBar.Progress > .5)
            {
                myProgressBar.Progress = 0;
                return false;
            }

            return true;
        });

Upvotes: 1

Related Questions