动画c#中的线段不起作用?

时间:2014-04-09 17:41:59

标签: c# wpf animation storyboard

这是我认为正确的代码。但它不起作用。这是某种错误还是我做错了什么?来自xaml的动画线段工作得很好。

MainWindow.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>

MainWindow.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();


    }
}

对我而言,它看起来像sb.Begin();没有触发或storyboard.settarget / setproperty设置不正确。但是,如果是这样的话会出现什么问题?

1 个答案:

答案 0 :(得分:1)

好吧这对我来说很愚蠢,但是使用这段代码而不是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.Animatable 无法通过使用故事板来控制......我认为应该纠正这种机制。