5

我使用此代码进行旋转

void rotate()
{
    Duration Time_duration = new Duration(TimeSpan.FromSeconds(20));
    Storyboard MyStory = new Storyboard();
    MyStory.Duration = Time_duration;
    DoubleAnimation My_Double = new DoubleAnimation();
    My_Double.Duration = Time_duration;
    MyStory.Chil*emphasized text*dren.Add(My_Double);
    RotateTransform MyTransform = new RotateTransform();
    Storyboard.SetTarget(My_Double, MyTransform);
    Storyboard.SetTargetProperty(My_Double, new PropertyPath("Angle"));
    My_Double.To = 270;
    this.maincan.RenderTransform = MyTransform;
    this.maincan.RenderTransformOrigin = new Point(0.5, 0.5);
    //stackPanel1.Children.Add(image1);
    MyStory.Begin();
}

它正在工作,但我想连续旋转图像。

4

2 回答 2

7

Storyboard有一个名为的属性,RepeatBehavior它允许您控制动画的重复方式。在您的代码中,添加以下行...

MyStory.RepeatBehavior = RepeatBehavior.Forever;  // repeat forever 

或者

MyStory.RepeatBehavior = new RepeatBehavior(TimeSpan.FromSeconds(20)); // repeat for 20 seconds

官方文档中还有更多示例:

http://msdn.microsoft.com/en-us/library/system.windows.media.animation.timeline.repeatbehavior(v=vs.95).aspx

于 2013-04-02T12:22:55.360 回答
4

这只是一个想法,但如果您唯一关心的是让图像不断旋转,这可能是最简单的方法。

        void rotate()
        {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += dispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

所以每一秒事件都会被触发

 private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        Storyboard MyStory = new Storyboard();
        MyStory.Duration = new TimeSpan(0, 0, 1);
        DoubleAnimation My_Double = new DoubleAnimation();
        My_Double.Duration = new TimeSpan(0, 0, 1);
        MyStory.Children.Add(My_Double);
        RotateTransform MyTransform = new RotateTransform();
        Storyboard.SetTarget(My_Double, MyTransform);
        Storyboard.SetTargetProperty(My_Double, new PropertyPath("Angle"));
        My_Double.To = 360;
        YourImage.RenderTransform = MyTransform;
        YourImage.RenderTransformOrigin = new Point(0.5, 0.5);       
        MyStory.Begin();
    }

让我知道事情的后续 (:

编辑

这只是一个想法,我相信有更好的方法

 void rotate(int i)
       {            
        Storyboard MyStory = new Storyboard();
        MyStory.Duration = new TimeSpan(0,0,1);           
        DoubleAnimation My_Double = new DoubleAnimation();
        My_Double.Duration =  new TimeSpan(0,0,1);          
        MyStory.Children.Add(My_Double);
        RotateTransform MyTransform = new RotateTransform();
        Storyboard.SetTarget(My_Double, MyTransform);
        Storyboard.SetTargetProperty(My_Double, new PropertyPath("Angle"));
        My_Double.From = i;
        My_Double.To = i +90;
        m_Image.RenderTransform = MyTransform;
        m_Image.RenderTransformOrigin = new Point(0.5, 0.5);            
        MyStory.Begin();
        MyStory.Completed +=((arg,c) =>
        {
            if (i == 360)
            {
                rotate(0);
            }
            else 
            {
                rotate(i + 90);
            }        
        });                                
    }
于 2013-04-02T10:15:43.093 回答