一段时间后,wpf动画中的减缓功能会自动关闭
本文关键字:功能 wpf 动画 一段时间 | 更新日期: 2023-09-27 17:57:44
WPF和c#。c代码中的动画。我有许多点(>1000)的路径动画。动画是通过程序创建的,使用一个故事板和具有轻松功能的点动画。重复行为设置为永远。一段时间后,放松功能关闭了。我认为这是某种优化。我可以关闭这个功能吗?这样轻松功能也会永远设置吗?
这是我的mainwindow_loaded函数。我的xaml代码中只有一个名为Grid的Grid。
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Random r = new Random(10);
Storyboard sb = new Storyboard();
Path p = new Path
{
Width = 500,
Height = 500,
Name = "Path",
Fill = Brushes.Green,
HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
Margin = new Thickness(25, 25, 0, 0),
VerticalAlignment = System.Windows.VerticalAlignment.Top
};
PathGeometry pg = new PathGeometry();
var pf = new PathFigure { StartPoint = new Point(0, 250) };
for (int i = 0; i < this.ActualWidth; i++)
{
LineSegment ls = new LineSegment { Point = new Point(i, 50) };
var pa = new PointAnimation
{
To = new Point(i, 80),
Duration = new Duration(new TimeSpan(0, 0, 0, 2)),
BeginTime = new TimeSpan(0, 0, 0, 0, i * 10), //+ r.Next(200)
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever,
EasingFunction = new SineEase { EasingMode = EasingMode.EaseInOut }
};
Storyboard.SetTargetProperty(pa, new PropertyPath("(Path.Data).(PathGeometry.Figures)[0].(PathFigure.Segments)[" + i + "].(LineSegment.Point)"));
Storyboard.SetTarget(pa, p);
sb.Children.Add(pa);
pf.Segments.Add(ls);
}
LineSegment endSegment = new LineSegment { Point = new Point(this.ActualWidth, 250) };
pf.Segments.Add(endSegment);
pg.Figures.Add(pf);
p.Data = pg;
grid.Children.Add(p);
sb.Begin();
}
试着把+r.Next(200)放在代码中,这样这行就会看起来像
BeginTime = new TimeSpan(0, 0, 0, 0, i * 10 + r.Next(200)),
然后缓解功能将永远不会消失。
我不确定这会有什么帮助,但为什么不为EasingFunction使用共享实例,而不是为每个动画创建一个单独的对象呢?从你的代码来看,它们似乎都一样?
我注意到的另一件事是,你在Loaded回调中立即开始动画,但根据我使用WPF和Silverlight的经验,Loaded事件对框架来说非常特殊,你应该非常小心在其中做什么
this.Dispatcher.BeginInvoke(new Action(() => db.BeginStoryboard()));