【发布时间】:2010-02-24 17:11:52
【问题描述】:
如何从 WPF 元素中删除正在运行的动画,使其 Completed 事件不会触发?
here 和 here 提出的解决方案移除了动画的可见效果,但 Completed 事件仍会在动画完成时触发。
这是一些演示我的问题的代码(它在带有标签、按钮和文本框的窗口后面的代码中):
int _count = 0;
private void button1_Click(object sender, RoutedEventArgs e) {
myLabel.Content = "Starting animation: " + _count++;
// Cancel any already-running animations and return
// the label opacity to 1.0.
myLabel.BeginAnimation(Label.OpacityProperty, null);
myLabel.Opacity = 1.0;
// Create a new animation to fade out the label.
DoubleAnimation opacityAnim = new DoubleAnimation(1.0, 0.0, TimeSpan.FromSeconds(2), FillBehavior.Stop) {
BeginTime = TimeSpan.FromSeconds(3)
};
opacityAnim.Completed += (sndr, ev) => {
myTextbox.Text += Environment.NewLine + "Completed fired.";
};
// Start the countdown/animation.
myLabel.BeginAnimation(Label.OpacityProperty, opacityAnim);
}
如何删除动画以使其不会引发 Completed 事件?
【问题讨论】: