我希望动画一个进度,因此完成一个循环需要1分钟。我的代码是:
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();
此栏需要1分钟才能完成一个循环,这不是我想要的。因此,如果我在40秒开始,则需要一分钟才能达到60秒。
我想要做的是从当前的秒值开始,每秒递增一次,当它达到60秒时,再次开始,所以如果从40秒开始,则需要20秒才能达到一分钟。 当它达到一分钟时,我希望它再次开始(在0)。当然,我可以说我正在开发一个时钟/计时器,但我想我会更好地解释运行代码时发生了什么。
当我将From值添加到newAnimation时,它总是从该值开始,而不是从BeginTime值开始。
有什么想法吗?
感谢。
答案 0 :(得分:0)
好吧,如果你想让你的动画花费一分钟以上的时间,你必须告诉它:
int from = 50;
int to = 60;
newAnimation.From = from;
newAnimation.To = to;
newAnimation.Duration = new Duration(TimeSpan.FromSeconds(to - from));
答案 1 :(得分:0)
感谢svick,我找到了如何做到这一点。解决方案如下:
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();
}