如何在wpf c#代码中使用Storyboard旋转网格

时间:2016-03-21 16:32:31

标签: c# wpf xaml

我想在wpf的c#代码中使用storyboard旋转简单网格 我的xaml代码是

class HomeController @Inject() (router: Provider[Router]) {
....
    router.get().documentation.foreach(println)
....
}

有网格和一个按钮 在按钮的onclick事件我使用

<Window x:Class="rotate_test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:rotate_test"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid Name="my_grid">
            <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="150,170,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
        </Grid>
    </Grid>
</Window>

但它不起作用 我的问题是什么? 如何为动画设置加速度? 日Thnx

1 个答案:

答案 0 :(得分:2)

您必须先初始化网格RenderTransform,然后才能为其设置动画。

<Grid Name="my_grid">
    <Grid.RenderTransform>
        <RotateTransform />
    </Grid.RenderTransform>
</Grid>

您也可以在没有故事板的情况下直接为RotateTransform制作动画。

只需给它一个名字

<RotateTransform x:Name="transform" />

并在背后的代码中为其设置动画,例如

transform.BeginAnimation(RotateTransform.AngleProperty, rotateAnimation);

除此之外,DoubleAnimation有几个控制加速和减速的属性,如AccelerationRatioDecelerationRatioEasingFunction