這是我的動畫倒計時代碼。當bandera_countdown == false時,定時器應停止並重置。它重置,但它繼續在後臺工作,因爲即使重置後,countdownStoryboard.Completed + = CountdownTimer_Completed也會觸發。重置倒計時動畫c#WPF
private void StartCountdown(FrameworkElement target)
{
var countdownAnimation = new StringAnimationUsingKeyFrames();
var countdownStoryboard = new Storyboard();
if (bandera_countdown == true)
{
for (var i = 10; i > 0; i--)
{
var keyTime = TimeSpan.FromSeconds(10 - i);
var frame = new DiscreteStringKeyFrame(i.ToString(), KeyTime.FromTimeSpan(keyTime));
countdownAnimation.KeyFrames.Add(frame);
}
countdownAnimation.KeyFrames.Add(new DiscreteStringKeyFrame(" ", KeyTime.FromTimeSpan(TimeSpan.FromSeconds(11))));
Storyboard.SetTargetName(countdownAnimation, target.Name);
Storyboard.SetTargetProperty(countdownAnimation, new PropertyPath(TextBlock.TextProperty));
countdownStoryboard.Children.Add(countdownAnimation);
countdownStoryboard.Completed += CountdownTimer_Completed;
countdownStoryboard.Begin(this);
}
else
{
countdownStoryboard.Stop();
countdownStoryboard.Remove();
}
}
我能做些什麼來使我的計時器停止並在10秒內再次開始? 謝謝!
謝謝! bandera_countdown設置爲false。問題是,當發生這種情況時,計時器重置爲10,但仍繼續運行。例如,倒計時結束時(等於0)會觸發一個事件處理程序,因此可以說當bandera_countdown設置爲false時,我的倒計時時間爲3秒,因此倒計數重置並顯示10秒,然後再次開始倒計時,但在3處理程序觸發的秒數。我不知道我是否自我解釋。 – user3634248 2014-09-04 17:15:42