如何仅使用 c# 代码(在 WPF 窗口内)在图像对象上创建旋转动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11073573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:22:53  来源:igfitidea点击:

How do I create a rotate animation on an image object using c# code only (inside a WPF window)

c#wpfanimationrotation

提问by

I have a couple of open questions relating to the same sort of thing,

我有几个关于同一类事情的开放性问题,

I am quite new to WPF but experienced with C# and Winforms.

我对 WPF 很陌生,但对 C# 和 Winforms 有经验。

I have looked around on the interweb for a working example but have yet to find one that works.

我在互联网上环顾四周寻找一个有效的例子,但还没有找到一个有效的例子。

What I want to achieve is in a C# function create the following

我想要实现的是在 C# 函数中创建以下内容

  • create an image (image 1)
  • create an image (image 2)
  • put the images on the window side by side
  • create a storyboard
  • animate the rotate property of image1 from 0 to 360 (animation1)
  • animate the opacity property of image 2 from full to invisible (animation2)
  • the storyboard should run for ten seconds with animation 1 starting at 0 seconds and animation 2 starting at 5 seconds
  • 创建图像(图像 1)
  • 创建图像(图像 2)
  • 将图像并排放在窗口上
  • 创建故事板
  • 动画 image1 的旋转属性从 0 到 360 (animation1)
  • 将图像 2 的不透明度属性从完全变为不可见(animation2)
  • 故事板应该运行十秒钟,动画 1 从 0 秒开始,动画 2 从 5 秒开始

apologies for the explicit request for code, but, I have looked, and tried, my previous question had full code that executed but no animation showed (link below)

对代码的显式请求表示歉意,但是,我已经查看并尝试过,我之前的问题有完整的代码可以执行但没有显示动画(下面的链接)

how to create a storyboard and rotating an image in wpf using c# code

如何使用 c# 代码在 wpf 中创建故事板和旋转图像

thanks in advance

提前致谢

Dan.

担。

采纳答案by Tim

Here is a working XAML versionn of your question followed by the identical thing in C#. May not be exactly what you were after, but it should illustrate it.

这是您问题的有效 XAML 版本,然后是 C# 中的相同内容。可能不完全是你所追求的,但它应该说明它。

XAML version:

XAML 版本:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Storyboard x:Key="Storyboard" BeginTime="00:00:00.000" Duration="00:00:10.000">
            <DoubleAnimation Storyboard.TargetName="RotateImage" 
                             Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" 
                             From="0" To="360" BeginTime="00:00:05.000" Duration="00:00:05.000" />
            <DoubleAnimation Storyboard.TargetName="OpacityImage" 
                             Storyboard.TargetProperty="Opacity" 
                             From="1" To="0" Duration="00:00:10.000" />
        </Storyboard>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Image x:Name="RotateImage" Stretch="Uniform" Source="Chrysanthemum.jpg">
            <Image.RenderTransform>
                <RotateTransform Angle="0" />
            </Image.RenderTransform>
        </Image>
        <Image x:Name="OpacityImage" Grid.Column="1" Stretch="Uniform" Source="Desert.jpg" />
        <Button Grid.Row="1" Grid.ColumnSpan="2" Content="Start">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard Storyboard="{StaticResource Storyboard}" />
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </Grid>
</Window>

And C# version:

和 C# 版本:

    public MainWindow()
    {
        InitializeComponent();

        Image rotateImage = new Image()
        {
            Stretch = Stretch.Uniform,
            Source = new BitmapImage(new Uri("pack://application:,,,/Chrysanthemum.jpg")),
            RenderTransform = new RotateTransform()
        };
        Image opacityImage = new Image()
        {
            Stretch = Stretch.Uniform,
            Source = new BitmapImage(new Uri("pack://application:,,,/Desert.jpg"))
        };

        LayoutRoot.Children.Add(rotateImage);
        LayoutRoot.Children.Add(opacityImage);

        Grid.SetColumn(opacityImage, 1);

        Storyboard storyboard = new Storyboard();
        storyboard.Duration = new Duration(TimeSpan.FromSeconds(10.0));
        DoubleAnimation rotateAnimation = new DoubleAnimation()
        {
            From = 0,
            To = 360,
            Duration = storyboard.Duration
        };
        DoubleAnimation opacityAnimation = new DoubleAnimation()
        {
            From = 1.0,
            To = 0.0,
            BeginTime = TimeSpan.FromSeconds(5.0),
            Duration = new Duration(TimeSpan.FromSeconds(5.0))
        };

        Storyboard.SetTarget(rotateAnimation, rotateImage);
        Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
        Storyboard.SetTarget(opacityAnimation, opacityImage);
        Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));

        storyboard.Children.Add(rotateAnimation);
        storyboard.Children.Add(opacityAnimation);

        Resources.Add("Storyboard", storyboard);

        Button button = new Button()
        {
            Content = "Begin"
        };
        button.Click += button_Click;

        Grid.SetRow(button, 1);
        Grid.SetColumnSpan(button, 2);

        LayoutRoot.Children.Add(button);
    }

    void button_Click(object sender, RoutedEventArgs e)
    {
        ((Storyboard)Resources["Storyboard"]).Begin();
    }

回答by m2pathan

Animation is work as follows.

动画工作如下。

1-The program creates a timer.

1-程序创建一个计时器。

2-The program checks the timer at set intervals to see how much time has elapsed.

2-程序以设定的时间间隔检查计时器以查看已经过去了多少时间。

3-Each time the program checks the timer, it computes the current opacity value for the rectangle based on how much time has elapsed.

3-每次程序检查计时器时,它都会根据经过的时间计算矩形的当前不透明度值。

4-The program then updates the rectangle with the new value and redraws it.

4-程序然后用新值更新矩形并重新绘制它。

Following is the Code which create Rectangle and animate it.

以下是创建矩形并为其设置动画的代码。

<Window x:Class="Animation.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Animated Rectangle" Height="350" Width="525">
<Grid>
    <StackPanel Margin="10">
        <Image Name="MyImage" Source="e:\a.jpg" Width="100" Margin="50" ></Image>
        <Rectangle
            Name="MyRectangle"
            Width="100" 
            Height="100"
            Fill="Blue">

            <Rectangle.Triggers>
                <!-- Animates the rectangle's opacity. -->
                <EventTrigger RoutedEvent="Rectangle.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                                Storyboard.TargetName="MyImage" 
                                Storyboard.TargetProperty="Opacity"
                                From="1.0" To="0.0" Duration="0:0:3" 
                                AutoReverse="True" RepeatBehavior="Forever" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Rectangle.Triggers>
        </Rectangle>
    </StackPanel>
</Grid>