大家好,新手问题,希望帮忙看看:
我在窗口中添了一个Image控件,我想让它在窗口中,不停的左右移动。
我在for循环里把它的margin属性不停的增减,结果程序会出现未响应的状态,等到循环完了一下子就跳到了最后的位置。
请问如何解决?

解决方案 »

  1.   

    不能像你这么做,你应该使用RenderTransform和动画来进行处理,上Google和百度搜一下,网上很多解决方案
      

  2.   

    我用StoryBoard可以实现动画效果了,但是我想让动画只播放一次,怎么转换为C#代码?              Storyboard story = new Storyboard();
                story.AutoReverse = true;
                story.RepeatBehavior = RepeatBehavior.Forever; //我想让它只播放一次,怎么改?            
                story.Children.Add(xAnimation);
      

  3.   

    参考RepeatBehavior设置
                   <!-- 创建一个无限循环的周期为2秒的动画. -->
                    <DoubleAnimation 
                      Storyboard.TargetName="ForeverRepeatingRectangle" 
                      Storyboard.TargetProperty="Width" 
                      From="50" To="300" Duration="0:0:2" RepeatBehavior="Forever" />                <!-- 创建一个周期为2秒的动画,该动画4秒钟重复一次. -->          
                    <DoubleAnimation 
                      Storyboard.TargetName="FourSecondsRepeatingRectangle" 
                      Storyboard.TargetProperty="Width"
                      From="50" To="300" Duration="0:0:2" RepeatBehavior="0:0:4" />                <!-- 创建一个周期为2秒的动画,重复执行2次. -->
                    <DoubleAnimation 
                      Storyboard.TargetName="TwiceRepeatingRectangle" 
                      Storyboard.TargetProperty="Width" 
                      From="50" To="300" Duration="0:0:2" RepeatBehavior="2x" />                     <!-- 创建一个周期为2秒的动画,仅执行一半,也就是执行1秒钟,To 150 -->
                    <DoubleAnimation 
                      Storyboard.TargetName="HalfRepeatingRectangle" 
                      Storyboard.TargetProperty="Width" 
                      From="50" To="300" Duration="0:0:2" RepeatBehavior="0.5x" />                <!-- Create an animation that repeats for one second. The resulting animation
                         plays for one second, half of its Duration. It animates from 50 to 150. -->
                    <DoubleAnimation 
                      Storyboard.TargetName="OneSecondRepeatingRectangle" 
                      Storyboard.TargetProperty="Width" 
                      From="50" To="300" Duration="0:0:2" RepeatBehavior="0:0:1" />    
      

  4.   

    这段代码如果改成C#的代码,应该怎么写?
                  DoubleAnimation xAnimation = new DoubleAnimation();
                xAnimation.Duration = new Duration(TimeSpan.FromSeconds(60));//这里我怎么改为让它重复2次?
      

  5.   


    xAnimation.RepeatBehavior = new System.Windows.Media.Animation.RepeatBehavior(2);
      

  6.   

    已解决了,
    xAnimation.RepeatBehavior = new RepeatBehavior(1);谢谢大家!