【问题标题】:WPF trigger animation from codeWPF 从代码触发动画
【发布时间】:2016-04-09 13:43:59
【问题描述】:

我正在阅读许多类似的问答,但我没有得到我正在寻找的答案。所以,我在 Microsoft Bled 中做“家庭作业”,我真的很喜欢故事板,我知道如何通过单击按钮来触发它们,但是有人知道如何在 c# 中启动动画,例如在 if 语句中。

提前感谢您的回答和时间!

【问题讨论】:

  • 你正在寻找这个thread。另一种方法是Storyboard sb = this.FindResource("Storyboard1") as Storyboard; if (sb != null){ BeginStoryboard(sb); }
  • 谢谢,您的解决方案有效 :) 您可以将其发布为答案吗?
  • 很高兴它有帮助。当然,我会把它作为答案发布。 :-)
  • 这能回答你的问题吗? Call a storyboard declared in xaml from c#
  • 这是stackoverflow.com/questions/3755651/… 的骗子。接受的答案确实链接到另一个问题。

标签: c# wpf blend


【解决方案1】:

你正在寻找这个thread


另一种方法是:

Storyboard sb = this.FindResource("Storyboard1") as Storyboard;
if (sb != null){ BeginStoryboard(sb); }

【讨论】:

  • 对不起,因为我在这里写你的评论,但你可能知道是否可以使用相同的动画反向使用这样的代码。例如关闭和打开动画。谢谢:)
  • 不确定这thread 是否对您有帮助,但您可以通过单击几下在混合中创建反向故事板。看看this。另一种选择是VisualStates
  • 再次感谢,我做了反向动画
  • 很高兴我能帮上忙。
【解决方案2】:
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);
    }
}

【讨论】:

    【解决方案3】:

    您可以从代码隐藏中访问故事板,方法是为其命名并引用该名称以使用 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();
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 2012-08-10
      • 2014-10-09
      • 1970-01-01
      • 2021-11-28
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      相关资源
      最近更新 更多