从c#设置线段动画不起作用
本文关键字:动画 不起作用 段动画 设置 | 更新日期: 2023-09-27 17:57:47
这是我认为正确的代码。但它不起作用。这是某种错误还是我做错了什么?从xaml设置线段动画效果很好。
主窗口.xaml:
<Window x:Class="XXX.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas Name="canvas">
</Canvas>
</Window>
主窗口.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
anim();
}
void anim()
{
Path path = new Path { Stroke = Brushes.Red, StrokeThickness = 1 };
PathGeometry pg = new PathGeometry();
PathFigureCollection pfc = new PathFigureCollection();
PathFigure pf = new PathFigure { StartPoint = new Point(50, 50) };
Storyboard sb = new Storyboard
{
Duration = System.Windows.Duration.Forever,
BeginTime = new TimeSpan(0, 0, 0),
RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever,
Name = "sb"
};
LineSegment ls = new LineSegment
{
IsSmoothJoin = true,
Point = new Point(80, 50)
};
PointAnimation pa = new PointAnimation
{
BeginTime = new TimeSpan(0, 0, 0, 0, 0),
Duration = new Duration(new TimeSpan(0, 0, 0, 1)),
From = new Point(0, 0),
To = new Point(0, 100),
RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever,
AutoReverse = true,
};
Storyboard.SetTarget(pa, ls);
Storyboard.SetTargetProperty(pa, new PropertyPath("Point"));
sb.Children.Add(pa);
pf.Segments.Add(ls);
pfc.Add(pf);
pg.Figures = pfc;
path.Data = pg;
canvas.Children.Add(path);
sb.Begin();
}
}
对我来说,它看起来像某人Begin();未激发或故事板。settarge/setproperty设置不正确。但如果真是这样,又有什么错呢?
好吧,这对我来说很愚蠢,但有了这个代码而不是sb.begin(),它就可以工作了:
int count = 0;
foreach(var f in pf.Segments)
{
f.BeginAnimation(LineSegment.PointProperty, (PointAnimation)sb.Children.ElementAt(count));
count++;
}
LineSegment没有BeginStoryboard方法。http://msdn.microsoft.com/en-us/library/system.windows.media.linesegment.aspx
它不是一个框架元素,故事板开始方法只适用于框架元素。。。
http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.controls.wpf.leftrightplacementtooltip.beginstoryboard(v=vs.110).aspx
我看不出任何逻辑解释,为什么某个东西是System.Windows.Media.Animation.Animable无法通过使用情节串连板进行控制。。。我认为应该纠正这种机制。