如何在后面的代码中动画变换 [英] How to Animate Transforms in code behind
问题描述
我有一个自定义按钮.出于某种原因,我想在后面的代码中应用转换.并且转换工作正常.但是当我尝试在变换上添加动画时,动画不会生效.我的代码如下.
I have a custom Button. For some reason, I would like to apply transforms in code behind. And The transforms work fine. But when I try to add animations on the transforms, the animations don't take effect. My code is as below.
public partial class MyButton : Button
{
private TranslateTransform translate = new TranslateTransform() { X = 200, Y = 100 };
public MyButton()
{
InitializeComponent();
this.RenderTransform = new TransformGroup()
{
Children =
{
this.translate
}
};
}
protected override void OnClick()
{
base.OnClick();
// Edit: I need to use Storyboard to synchronized animation1 and animation2.
var sb = new Storyboard();
var animation1 = new DoubleAnimation(200, -10, new Duration(new TimeSpan(0, 0, 0, 1, 0)));
Storyboard.SetTarget(animation1, this.translate);
Storyboard.SetTargetProperty(animation1, new PropertyPath("X"));
sb.Children.Add(animation1);
var animation2 = new DoubleAnimation(100, 0, new Duration(new TimeSpan(0, 0, 0, 1, 0)));
Storyboard.SetTarget(animation2, this.translate);
Storyboard.SetTargetProperty(animation2, new PropertyPath("Y"));
sb.Children.Add(animation2);
sb.Begin(this);
}
}
谁能解释为什么以及如何为这个场景添加动画?
Could anyone explain why and how to add animations for this scenario?
推荐答案
OP 说他毕竟需要一个故事板.问题似乎是有时 SetTarget()
不起作用.不知道为什么,我没仔细看.我刚刚测试了解决方法,发现它很好.
OP says he needs a storyboard after all. The trouble with that seems to be that sometimes SetTarget()
doesn't work. I don't know why not, I didn't look into that. I just tested the workaround, and found it good.
请注意,您必须在构造函数中执行 NameScope
/RegisterName
内容.
Note that you have to do the NameScope
/RegisterName
stuff in the constructor.
public MyButton()
{
InitializeComponent();
this.RenderTransform = new TransformGroup()
{
Children =
{
this.translate
}
};
NameScope.SetNameScope(this, new NameScope());
RegisterName("translate", this.translate);
}
protected override void OnClick()
{
base.OnClick();
var sb = new Storyboard();
var animation1 = new DoubleAnimation(200, -10, new Duration(new TimeSpan(0, 0, 0, 1, 0)));
Storyboard.SetTargetName(animation1, "translate");
Storyboard.SetTargetProperty(animation1, new PropertyPath(TranslateTransform.XProperty));
sb.Children.Add(animation1);
var animation2 = new DoubleAnimation(100, 0, new Duration(new TimeSpan(0, 0, 0, 1, 0)));
Storyboard.SetTargetName(animation2, "translate");
Storyboard.SetTargetProperty(animation2, new PropertyPath(TranslateTransform.YProperty));
sb.Children.Add(animation2);
sb.Begin(this);
}
简单的解决方案
您不需要使用相同的参数创建两个不同的动画.事实证明,您也不需要故事板;那是为了开始一个事件或其他东西的动画.我不知道你为什么不谷歌这个.谷歌在wpf 故事板动画..."中自动完成了它.无论如何都在这里.
protected override void OnClick()
{
base.OnClick();
var animation1 = new DoubleAnimation(100, 0,
new Duration(new TimeSpan(0, 0, 0, 1, 0)));
translate.BeginAnimation(TranslateTransform.XProperty, animation1);
translate.BeginAnimation(TranslateTransform.YProperty, animation1);
}
这篇关于如何在后面的代码中动画变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!