【问题标题】:Convert XAML Storyboard to C#?将 XAML 故事板转换为 C#?
【发布时间】:2017-01-19 10:58:54
【问题描述】:

我在谷歌搜索后想知道是否有一个漫游工具可以将 Storyboard XAML 代码转换为 C#,或者是否有其他方法可以实现我的目标:

我需要后面的代码,因为我需要控制“命运之轮”将落在哪个值上,但是使用计时器和旋转事件创建动画太紧张了,而且生涩。

关于如何从 XAML Blend Storyboards 中获得黄油般平滑的动画,但对这种类型的事情将 C# 控制在一个中,有什么建议吗?

<Storyboard x:Key="OnMouseLeftButtonDown1">
        <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" Storyboard.TargetName="DailySpinColoursText_png">
            <EasingPointKeyFrame KeyTime="0" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:0.3" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:0.6" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:1.4" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:1.8" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:2.2" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:2.6" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:3" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:3.4" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:3.8" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:4.2" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:4.6" Value="0.5,0.5"/>
            <EasingPointKeyFrame KeyTime="0:0:5" Value="0.5,0.5"/>
        </PointAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="DailySpinColoursText_png">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="100"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="400"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1100"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1.4" Value="2600"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1.8" Value="4600"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2.2" Value="6100"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2.6" Value="7300"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="8300"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3.4" Value="8800"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3.8" Value="9000"/>
            <EasingDoubleKeyFrame KeyTime="0:0:4.2" Value="9100"/>
            <EasingDoubleKeyFrame KeyTime="0:0:4.6" Value="9150"/>
            <EasingDoubleKeyFrame KeyTime="0:0:5" Value="9200"/>
            <EasingDoubleKeyFrame KeyTime="0:0:5.4" Value="9220"/>
            <EasingDoubleKeyFrame KeyTime="0:0:5.8" Value="9225"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

提前谢谢你!

PS。我在网上找到了一些 C# 旋转动画的例子,但我需要它慢慢旋转,然后在动画中间开始快速旋转,然后在最后下降到一个缓慢而悬疑的结尾。根据我在互联网上看到的东西,我不知道该怎么做。

【问题讨论】:

  • 第一件事是删除 RenderTranformOrigin 的动画。它什么都不做。

标签: c# wpf xaml storyboard blend


【解决方案1】:

您可以在后面的代码中为 RotateTransform 的 Angle 属性设置动画,如下所示。除了使用很多 EasingDoubleKeyFrames,您还可以使用单个 EasingFunction:

element.RenderTransformOrigin = new Point(0.5, 0.5);
element.RenderTransform = new RotateTransform();

var animation = new DoubleAnimation
{
    To = 9225,
    Duration = TimeSpan.FromSeconds(5.8),
    EasingFunction = new CubicEase { EasingMode = EasingMode.EaseInOut }
};

element.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, animation);

【讨论】:

  • 你真的是最棒的,非常感谢!我将使用您的代码并对其进行拼图,以准确了解它的作用,即使它看起来很简单。我真的很喜欢 WPF 的这些动画功能。
猜你喜欢
  • 2015-09-20
  • 2012-01-17
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
相关资源
最近更新 更多