Stackpanel动画问题

时间:2012-11-26 17:48:25

标签: c# .net wpf animation

我尝试使用CanvasStackpanel实现不同高度的不同控件集的动画。

所以我填充Stackpanel并应用所有设置,但无论如何在开始动画中跳跃一段时间,只有在此过程顺利完成后才会再次跳跃2-3次等等....

任何线索为什么会这样?我使用经典的双动画等...

感谢任何一个人!

<Canvas  ClipToBounds="True" Name="canMain" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
   <StackPanel  Name="tbmarquee" HorizontalAlignment="Stretch"  ></StackPanel>
</Canvas> 

private void BottomToTopMarquee()
{
     tbmarquee.Orientation = Orientation.Vertical;  
     DoubleAnimation doubleAnimation = new DoubleAnimation();
     doubleAnimation.From = -tbmarquee.ActualHeight;
     doubleAnimation.To = canMain.ActualHeight;
     doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
     doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(_marqueeTimeInSeconds));
     tbmarquee.BeginAnimation(Canvas.BottomProperty, doubleAnimation);
 }

我甚至尝试过这样的

   Thread thread = new Thread(new ThreadStart(
            delegate()
            {
                DispatcherOperation dispatcherOp =
                  this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(
                    delegate()
                    {

                        DoubleAnimation doubleAnimation = new DoubleAnimation();
                        doubleAnimation.From = -tbmarquee.ActualHeight;
                        doubleAnimation.To = canMain.ActualHeight;
                        doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
                        doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(_marqueeTimeInSeconds));
                        tbmarquee.BeginAnimation(Canvas.BottomProperty, doubleAnimation);
                    }));
                dispatcherOp.Completed += new EventHandler(DispatcherOpCompleted);
            }));
            thread.Start();

我的意思是动画没有顺利开始跳跃......但后来很顺利......

1 个答案:

答案 0 :(得分:2)

当我遇到动画性能问题时,我通常可以通过降低FrameRate并缓解渲染系统的需求来缓解它们。在我的Application启动方法中,我添加了以下内容:

MediaTimeline.DesiredFrameRateProperty.OverrideMetadata(typeof(System.Windows.Media.Animation.Timeline), new FrameworkPropertyMetadata(10));

WPF默认值为60(这很荒谬,因为即使现代视频也不会超过30 FPS)。我通常会在10左右找到良好的性能平衡,但您可以调整此数字,直到您的动画看起来和按照您的意愿执行。

我希望这会有所帮助。