作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个简单的程序,它为 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);
}
}
}
这是一个类似的程序,它为 LineGeometry
的 EndPoint
设置动画,这是 Path
的 Data
:
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/
这是一个简单的程序,它为 Line 形状的 Y2 属性设置动画。请注意,我使用 SetTarget 方法来定位 Line。这个程序运行良好。 using System; using System.Wi
为什么 Storyboard.SetTargetName 有效而 Storyboard.SetTarget 无效?这里 xaml -
我是一名优秀的程序员,十分优秀!