简体   繁体   English

如何通过 C# 代码(不是 XAML)旋转在 WPF 中单击按钮时动画的图像

[英]How do i rotate an image animated on button click in WPF through C# code (Not XAML)

I dont know if im approaching this wrong or if there is an error in my code.我不知道我是否接近这个错误或者我的代码中是否有错误。

And as i said above i would like to solve the problem through C# code and not in XAML.正如我上面所说,我想通过 C# 代码而不是 XAML 来解决问题。

I don't get any compiler errors it just does nothing.我没有收到任何编译器错误,它什么也不做。

XAML: XAML:

    <Button Content="Rotate" HorizontalAlignment="Left" Margin="1007,637,0,0" VerticalAlignment="Top" Click="Button_Click" Height="36" Width="51"/>
    <Image x:Name="Brett" Margin="0,-563,0,187" Stretch="Fill" Source="/Monopoly_Brett_bib.png" RenderTransformOrigin="0.5, 0.5" OpacityMask="White"></Image>

C#: C#:

private void Button_Click(object sender, RoutedEventArgs e)
    {

        DoubleAnimation doubleAnimation = new DoubleAnimation();
        doubleAnimation.Duration = new Duration(new TimeSpan(0, 0, 0, 3, 0));
        Storyboard storyBoard = new Storyboard();
        storyBoard.Children.Add(doubleAnimation);
        Storyboard.SetTarget(doubleAnimation, Brett);
        Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(RotateTransform.AngleProperty));
        doubleAnimation.From = 0;
        doubleAnimation.To = -90;
        storyBoard.Begin();
        storyBoard.Completed += StoryBoard_Completed;

    }

Try this:尝试这个:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        DoubleAnimation doubleAnimation = new DoubleAnimation
        {
            From =0,
            To = -90,
            Duration = new Duration(TimeSpan.FromSeconds(3))
        };
        Storyboard storyBoard = new Storyboard();

        Brett.RenderTransform = new RotateTransform();

        storyBoard.Children.Add(doubleAnimation);
        Storyboard.SetTarget(doubleAnimation, Brett);
        Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("RenderTransform.Angle"));

        storyBoard.Begin();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM