我有一个WPF画布,我正在从代码动态创建对象。这些对象通过设置RenderTransform属性进行转换,并且需要在其中一个转换上应用动画。目前,我无法使任何转换的属性动画化(尽管不会引发任何异常并且动画似乎在运行 - 完成事件被触发)。
此外,如果动画系统受到压力,有时Storyboard.Completed事件永远不会被触发。
我遇到的所有示例都是从XAML中对转换进行动画处理。MSDN文档建议必须设置转换的x:Name属性才能进行动画处理,但我没有找到一种可行的方法来从代码中设置它。
有什么想法吗?
以下是完整的代码清单,可重现该问题:
此外,如果动画系统受到压力,有时Storyboard.Completed事件永远不会被触发。
我遇到的所有示例都是从XAML中对转换进行动画处理。MSDN文档建议必须设置转换的x:Name属性才能进行动画处理,但我没有找到一种可行的方法来从代码中设置它。
有什么想法吗?
以下是完整的代码清单,可重现该问题:
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace AnimationCompletedTest {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
Canvas panel;
public MainWindow() {
InitializeComponent();
MouseDown += DoDynamicAnimation;
Content = panel = new Canvas();
}
void DoDynamicAnimation(object sender, MouseButtonEventArgs args) {
for (int i = 0; i < 12; ++i) {
var e = new Ellipse {
Width = 16,
Height = 16,
Fill = SystemColors.HighlightBrush
};
Canvas.SetLeft(e, Mouse.GetPosition(this).X);
Canvas.SetTop(e, Mouse.GetPosition(this).Y);
var tg = new TransformGroup();
var translation = new TranslateTransform(30, 0);
tg.Children.Add(translation);
tg.Children.Add(new RotateTransform(i * 30));
e.RenderTransform = tg;
panel.Children.Add(e);
var s = new Storyboard();
Storyboard.SetTarget(s, translation);
Storyboard.SetTargetProperty(s, new PropertyPath(TranslateTransform.XProperty));
s.Children.Add(
new DoubleAnimation(3, 100, new Duration(new TimeSpan(0, 0, 0, 1, 0))) {
EasingFunction = new PowerEase {EasingMode = EasingMode.EaseOut}
});
s.Completed +=
(sndr, evtArgs) => {
Debug.WriteLine("Animation {0} completed {1}", s.GetHashCode(), Stopwatch.GetTimestamp());
panel.Children.Remove(e);
};
Debug.WriteLine("Animation {0} started {1}", s.GetHashCode(), Stopwatch.GetTimestamp());
s.Begin();
}
}
[STAThread]
public static void Main() {
var app = new Application();
app.Run(new MainWindow());
}
}
}