开发者

WPF - animate progress bar according to time

I'm looking to animate a progress, so that it takes 1 minute to complete a cycle. My code is:

ProgressBar _progressSec = new ProgressBar();
_progressSec.Name = "_progressSec";
_progressSec.Minimum = 0;
_progressSec.Maximum = 60;
_progressSec.Value = DateTime.Now.Second;
this.RegisterName(_progressSec.Name, _progressSec);

NewGrid.Children.Add(_progressSec);

Storyboard newStory = new Storyboard();
DoubleAnimation newAnimation = new DoubleAnimation();
newAnimation.To = 60;
newAnimation.BeginTime = new TimeSpan(DateTime.Now.Second);
newAnimation.RepeatBehavior = RepeatBehavior.Forever;
newAnimation.Duration = new Duration(TimeSpan.FromMinutes(1));
newStory.Children.Add(newAnimation);

Storyboard.SetTarget(newAnimation, _progressSec);
Storyboard.SetTargetProperty(newAnimation, new PropertyPath(ProgressBar.ValueProperty));

newStory.Begin();

This bar takes exactly 1 minute to complete a cycle, which is not exactly what I want. So if I start it at 40 seconds, it'll take a minute to reach 60 seconds.

What I'd like it to do is to start at the current seconds value, increment each second, and when it reaches 60 seconds, start again, so if it starts at 40 seconds it takes 20 seconds to reach a minute. When it reaches a minute, I开发者_开发知识库'd like it to start again (at 0). Of course, I could have just said I'm developing a clock/timer, but thought I'd better explain what was happening when I run the code.

When I add a From value to newAnimation, it always starts at that value, not at the BeginTime value.

Any ideas?

Thanks.


Well, if you want your animation to take some other time than one minute, you have to tell it so:

int from = 50;
int to = 60;
newAnimation.From = from;
newAnimation.To = to;
newAnimation.Duration = new Duration(TimeSpan.FromSeconds(to - from));


Thanks to svick, I worked out how to do it. Solution is below:

    ProgressBar _progressSec = new ProgressBar();
    DoubleAnimation newAnimation = new DoubleAnimation();
    Storyboard newStory = new Storyboard();

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        _progressSec.Name = "_progressSec";
        _progressSec.Minimum = 0;
        _progressSec.Maximum = 59;
        _progressSec.Value = DateTime.Now.Second;
        this.RegisterName(_progressSec.Name, _progressSec);

        NewGrid.Children.Add(_progressSec);

        int from = DateTime.Now.Second;
        int to = 59;
        newAnimation.From = from;
        newAnimation.To = to;
        newAnimation.Duration = new Duration(TimeSpan.FromSeconds(to - from));

        newStory.Completed += new EventHandler(story_Completed);
        newStory.Children.Add(newAnimation);

        Storyboard.SetTarget(newAnimation, _progressSec);
        Storyboard.SetTargetProperty(newAnimation, new PropertyPath(ProgressBar.ValueProperty));

        newStory.Begin();
    }

    void story_Completed(object sender, EventArgs e)
    {
        int from2 = 0;
        int to2 = 59;
        newAnimation.From = from2;
        newAnimation.To = to2;
        newAnimation.Duration = new Duration(TimeSpan.FromMinutes(1));

        newStory.Children.Add(newAnimation);

        Storyboard.SetTarget(newAnimation, _progressSec);
        Storyboard.SetTargetProperty(newAnimation, new PropertyPath(ProgressBar.ValueProperty));

        newStory.Begin();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜