WPF 轨迹动画

1.后台

  public MainWindow()
        {
            InitializeComponent();
            /// <summary>
            /// Window2.xaml 的交互逻辑
            /// </summary>
            //var canvas = new Canvas();

            //Content = canvas; 
            var points =
                new List<Point>()
                {
                    new Point(10, 10),
                    new Point(10, 90),
                    new Point(70, 90)
                };
            var sb = new Storyboard();
            sb.Completed += Sb_Completed;
            for (int i = 0; i < points.Count - 1; i++)
            {
                var lineGeometry = new LineGeometry(points[i], points[i]);
                var path =
                    new Path()
                    {
                        Stroke = Brushes.Red,
                        StrokeThickness = 2,
                        Data = lineGeometry
                    };
                canvas.Children.Add(path);
                var animation =
                    new PointAnimation(points[i], points[i + 1], new Duration(TimeSpan.FromMilliseconds(1000)))
                    {
                        BeginTime = TimeSpan.FromMilliseconds(i * 1010)
                    };
                sb.Children.Add(animation);
                RegisterName("geometry" + i, lineGeometry);
                Storyboard.SetTargetName(animation, "geometry" + i);
                Storyboard.SetTargetProperty(animation, new PropertyPath(LineGeometry.EndPointProperty));
            }
            MouseDown += (s, e) => sb.Begin(this);
        }

        private void Sb_Completed(object sender, EventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(delegate { lblTest.Content = "测试哦"; }));
        }

2.xaml

   <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="160px">
            </RowDefinition>
            <RowDefinition Height="*" x:Name="row1">
            </RowDefinition>
            <RowDefinition Height="120">
            </RowDefinition>
        </Grid.RowDefinitions>
        <Canvas Grid.Row="1" x:Name="canvas"></Canvas>
        <Label Grid.Row="2" Name="lblTest"></Label>
    </Grid>

 

posted on 2021-05-31 10:01  郭小四  阅读(392)  评论(0编辑  收藏  举报