我怎样在一个图像对象仅使用C#code(WPF窗口中)创建一个旋转的动画 [英] How do I create a rotate animation on an image object using c# code only (inside a WPF window)

查看:433
本文介绍了我怎样在一个图像对象仅使用C#code(WPF窗口中)创建一个旋转的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个与同一类的事情,

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

我很新的WPF,但与C#和WinForms经验。

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

我看了看周围的interweb一个工作的例子,但还没有找到一个工作。

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

我想实现的是在C#函数创建以下

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


  • 创建一个图像(图像1)

  • 创建一个图像(图像2)

  • 将图像窗口并排

  • 创建一个故事板

  • 此搜索的从0旋转属性设置动画到360(动画1)

  • 从全动画图像2的不透明度属性无形(animation2)

  • 故事板应十秒动画1开始运行
    在0秒,动画2日开始在5秒。

道歉为code中的明确要求,但是,我已经看过,并试图,我的previous问题有充分的code,它执行,但没有动画显示(下面的链接)

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)

<一个href=\"http://stackoverflow.com/questions/11057127/how-to-create-a-storyboard-and-rotating-an-image-in-wpf-using-c-$c$c/11058289#11058289\">how创建一个故事板,并使用C#code 在WPF旋转图像。

在此先感谢

推荐答案

下面是一个工作XAML versionn你的问题,然后在C#中的相同的事情。可能不是你是什么之后,但它应该说明吧。

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版本:

<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>

和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();
    }

这篇关于我怎样在一个图像对象仅使用C#code(WPF窗口中)创建一个旋转的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆