gpt4 book ai didi

c# - SetTarget 与 RegisterName/SetTargetName

转载 作者:行者123 更新时间:2023-11-30 17:57:29 26 4
gpt4 key购买 nike

这是一个简单的程序,它为 Line 形状的 Y2 属性设置动画。请注意,我使用 SetTarget 方法来定位 Line。这个程序运行良好。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SoGeneratingAnimatedLine
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

var canvas = new Canvas();

Content = canvas;

var sb = new Storyboard();

var line = new Line()
{
X1 = 10, Y1 = 10,
X2 = 90, Y2 = 10,
Stroke = Brushes.Black,
StrokeThickness = 2
};

canvas.Children.Add(line);

var animation = new DoubleAnimation(10, 90, new Duration(TimeSpan.FromMilliseconds(1000)));

sb.Children.Add(animation);

Storyboard.SetTarget(animation, line);
Storyboard.SetTargetProperty(animation, new PropertyPath(Line.Y2Property));

MouseDown += (s, e) => sb.Begin(this);
}
}
}

这是一个类似的程序,它为 LineGeometryEndPoint 设置动画,这是 PathData:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SoGeneratingAnimatedLine
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

var canvas = new Canvas();

Content = canvas;

var sb = new Storyboard();

var lineGeometry =
new LineGeometry(new Point(10, 10), new Point(90, 10));

var path = new Path()
{
Stroke = Brushes.Black,
StrokeThickness = 2,
Data = lineGeometry
};

canvas.Children.Add(path);

var animation =
new PointAnimation(
new Point(90, 10),
new Point(90, 90),
new Duration(TimeSpan.FromMilliseconds(1000)));

sb.Children.Add(animation);

Storyboard.SetTarget(animation, lineGeometry);
Storyboard.SetTargetProperty(animation, new PropertyPath(LineGeometry.EndPointProperty));

MouseDown += (s, e) => sb.Begin(this);
}
}
}

第二个版本有效。但是,如果我替换行:

Storyboard.SetTarget(animation, lineGeometry);

与:

RegisterName("geometry", lineGeometry);
Storyboard.SetTargetName(animation, "geometry");

然后动画运行。

为什么第二个程序的 SetTarget 版本不起作用?什么时候可以使用 SetTarget 而不是 RegisterName/SetTargetName 组合?这两种方法有什么区别?

最佳答案

根本不需要 Storyboard。只需调用 BeginAnimation直接在 LineGeometry 上:

lineGeometry.BeginAnimation(LineGeometry.EndPointProperty, animation);

关于c# - SetTarget 与 RegisterName/SetTargetName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13217221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com