WPF 从代码触发动画
WPF trigger animation from code
我正在阅读许多类似的问答,但没有得到我正在寻找的答案。所以,我在 Microsoft Bled 中制作 "homework",我真的很喜欢故事板,我知道如何通过单击按钮触发它们,但是有谁知道如何在 c# 中启动动画,例如在 if 语句中。
感谢您的回答和提前花时间!
您可以通过为其命名并引用该名称以使用 Begin
方法从代码隐藏访问故事板。
<Canvas MouseLeftButtonDown="Handle_MouseDown"
Background="Gray" Width="600" Height="500">
<Canvas.Resources>
<Storyboard x:Name="myStoryboard">
<PointAnimation
x:Name="myPointAnimation"
Storyboard.TargetProperty="Center"
Storyboard.TargetName="MyAnimatedEllipseGeometry"
Duration="0:0:2"/>
</Storyboard>
</Canvas.Resources>
<Path Fill="Blue">
<Path.Data>
<EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
Center="200,100" RadiusX="15" RadiusY="15" />
</Path.Data>
</Path>
</Canvas>
代码隐藏:
private void Handle_MouseDown(object sender, MouseButtonEventArgs e)
{
// Retrieve current mouse coordinates.
double newX = e.GetPosition(null).X;
double newY = e.GetPosition(null).Y;
Point myPoint = new Point();
myPoint.X = newX;
myPoint.Y = newY;
myPointAnimation.To = myPoint;
myStoryboard.Begin();
}
public static class AnimationHelper
{
private static void AnimateOpacity(DependencyObject target, double from, double to)
{
var opacityAnimation = new DoubleAnimation
{
From = from,
To = to,
Duration = TimeSpan.FromMilliseconds(500)
};
Storyboard.SetTarget(opacityAnimation, target);
Storyboard.SetTargetProperty(opacityAnimation, "Opacity");
var storyboard = new Storyboard();
storyboard.Children.Add(opacityAnimation);
storyboard.Begin();
}
/// <summary>
/// Fades in the given dependency object.
/// </summary>
/// <param name="target">The target dependency object to fade in.</param>
public static void FadeIn(DependencyObject target)
{
AnimateOpacity(target, 0, 1);
}
}
您正在寻找这个 thread。
另一种方式是:
Storyboard sb = this.FindResource("Storyboard1") as Storyboard;
if (sb != null){ BeginStoryboard(sb); }
我正在阅读许多类似的问答,但没有得到我正在寻找的答案。所以,我在 Microsoft Bled 中制作 "homework",我真的很喜欢故事板,我知道如何通过单击按钮触发它们,但是有谁知道如何在 c# 中启动动画,例如在 if 语句中。
感谢您的回答和提前花时间!
您可以通过为其命名并引用该名称以使用 Begin
方法从代码隐藏访问故事板。
<Canvas MouseLeftButtonDown="Handle_MouseDown"
Background="Gray" Width="600" Height="500">
<Canvas.Resources>
<Storyboard x:Name="myStoryboard">
<PointAnimation
x:Name="myPointAnimation"
Storyboard.TargetProperty="Center"
Storyboard.TargetName="MyAnimatedEllipseGeometry"
Duration="0:0:2"/>
</Storyboard>
</Canvas.Resources>
<Path Fill="Blue">
<Path.Data>
<EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
Center="200,100" RadiusX="15" RadiusY="15" />
</Path.Data>
</Path>
</Canvas>
代码隐藏:
private void Handle_MouseDown(object sender, MouseButtonEventArgs e)
{
// Retrieve current mouse coordinates.
double newX = e.GetPosition(null).X;
double newY = e.GetPosition(null).Y;
Point myPoint = new Point();
myPoint.X = newX;
myPoint.Y = newY;
myPointAnimation.To = myPoint;
myStoryboard.Begin();
}
public static class AnimationHelper
{
private static void AnimateOpacity(DependencyObject target, double from, double to)
{
var opacityAnimation = new DoubleAnimation
{
From = from,
To = to,
Duration = TimeSpan.FromMilliseconds(500)
};
Storyboard.SetTarget(opacityAnimation, target);
Storyboard.SetTargetProperty(opacityAnimation, "Opacity");
var storyboard = new Storyboard();
storyboard.Children.Add(opacityAnimation);
storyboard.Begin();
}
/// <summary>
/// Fades in the given dependency object.
/// </summary>
/// <param name="target">The target dependency object to fade in.</param>
public static void FadeIn(DependencyObject target)
{
AnimateOpacity(target, 0, 1);
}
}
您正在寻找这个 thread。
另一种方式是:
Storyboard sb = this.FindResource("Storyboard1") as Storyboard;
if (sb != null){ BeginStoryboard(sb); }