在 WPF 中启动 XAML 动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19523905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting a XAML animation in WPF
提问by Paul Michaels
I'm using thisas a basis to make an animation start using code behind. Based on the contents of the article, I have the following:
我以此为基础,开始使用隐藏代码制作动画。根据文章的内容,我有以下几点:
<Window.Resources>
<Storyboard x:Key="sbdLabelRotation">
<DoubleAnimation
Storyboard.TargetName="lblHello"
Storyboard.TargetProperty="(TextBlock.RenderTransform).(RotateTransform.Angle)"
From="0"
To="360"
Duration="0:0:0.5"
RepeatBehavior="4x" />
</Storyboard>
</Window.Resources>
I have the following XAML (obviously):
我有以下 XAML(显然):
<Label x:Name="lblHello" Content="test" Margin="20"/>
And the code behind:
以及背后的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void AnimateLabelRotation()
{
Storyboard sbdLabelRotation = (Storyboard)FindResource("sbdLabelRotation");
sbdLabelRotation.Begin(this);
}
Which I call from a button click event. The FindResourceworks and finds the storyboard, but nothing happens. I have managed to get the animation to work on an event trigger, but clearly I'm missing something for the code behind.
我从按钮单击事件调用。在FindResource工作和发现故事板,但没有任何反应。我已经设法让动画在事件触发器上工作,但显然我遗漏了背后的代码。
采纳答案by XAMeLi
This:
这个:
<Label x:Name="lblHello" Content="test" Margin="20"/>
and this:
和这个:
Storyboard.TargetProperty="(TextBlock.RenderTransform).(RotateTransform.Angle)"
are not compatible.
不兼容。
When the animation tries to find the property to animate, it goes to (TextBlock.RenderTransform)and finds null since you didn't declare it (actually it doesn't since you say TextBlock but apply it to Label, more on that later in the answer). Thus it cannot find .(RotateTransform.Angle).
当动画尝试查找要设置动画的属性时,它会转到(TextBlock.RenderTransform)并找到 null,因为您没有声明它(实际上它没有,因为您说的是 TextBlock,而是将其应用于 Label,更多内容在后面的答案中介绍)。因此它找不到.(RotateTransform.Angle).
To remedy the issue:
要解决此问题:
<Label x:Name="lblHello"
Content="test"
Margin="20"
RenderTransformOrigin="0.5,0.5">
<Label.RenderTransform>
<RotateTransform />
</Label.RenderTransform>
</Label>
Notice RenderTransformOriginsetting - this means that the axis of rotation will be in the center of the object (X and Y).
注意RenderTransformOrigin设置 - 这意味着旋转轴将位于对象的中心(X 和 Y)。
Also, in the animation it should be:
此外,在动画中它应该是:
Storyboard.TargetProperty="(Label.RenderTransform).(RotateTransform.Angle)"
回答by outofdabox
There is a link to download the whole project http://www.galasoft.ch/mydotnet/articles/resources/article-2006102701/GalaSoftLb.Article2006102701.zip
有一个链接可以下载整个项目 http://www.galasoft.ch/mydotnet/articles/resources/article-2006102701/GalaSoftLb.Article2006102701.zip
You can study the code and see it running. Sometimes it's more helpful. Also in your code the part:
您可以研究代码并查看它的运行情况。有时它更有帮助。同样在您的代码中的部分:
sbdLabelRotation.Begin(this);
sbdLabelRotation.Begin(this);
could be wrong. As you know the thiskeyword references the class itself, in your case the MainWindowclass. You should try without the this keyword.
可能是错的。如您所知,this关键字引用类本身,在您的情况下是MainWindow类。您应该尝试不使用 this 关键字。

