0

如何让 WPF 的情节提要在启动后的预定义时间停止/暂停?例如开始后 20 秒?

4

1 回答 1

0

如果我正确理解了您的问题,从我的项目中,我会在定义的秒内将 UI 元素从一个元素平滑地更改为另一个元素。

void ScreenSelector(FrameworkElement CurrentElement, FrameworkElement ToNextElement, bool Side)
  {
   if (_Storyboard != null) _Storyboard.Stop(this);

   Thickness _TicknessLeft = new Thickness(Width, 0, -Width, 0);
   Thickness _TicknessRight = new Thickness(-Width, 0, Width, 0);
   Thickness _TicknessClient = new Thickness(0, 0, 0, 0);

   TimeSpan _TimeSpanStarting = TimeSpan.FromSeconds(0);
   TimeSpan _TimeSpanStopping = TimeSpan.FromSeconds(0.5);

   KeyTime _KeyTimeStarting = KeyTime.FromTimeSpan(_TimeSpanStarting);
   KeyTime _KeyTimeStopping = KeyTime.FromTimeSpan(_TimeSpanStopping);

   ToNextElement.Margin = Side ? _TicknessRight : _TicknessLeft;
   ToNextElement.Visibility = Visibility.Visible;

   Storyboard _StoryboardTemp = new Storyboard();

   ThicknessAnimationUsingKeyFrames _CurrentThicknessAnimationUsingKeyFrames = new ThicknessAnimationUsingKeyFrames();

   _CurrentThicknessAnimationUsingKeyFrames.BeginTime = _TimeSpanStarting;
   Storyboard.SetTargetName(_CurrentThicknessAnimationUsingKeyFrames, CurrentElement.Name);
   Storyboard.SetTargetProperty(_CurrentThicknessAnimationUsingKeyFrames, new PropertyPath("(FrameworkElement.Margin)"));
   _CurrentThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(_TicknessClient, _KeyTimeStarting));
   _CurrentThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(Side ? _TicknessLeft : _TicknessRight, _KeyTimeStopping));

   _StoryboardTemp.Children.Add(_CurrentThicknessAnimationUsingKeyFrames);

   ThicknessAnimationUsingKeyFrames _NextThicknessAnimationUsingKeyFrames = new ThicknessAnimationUsingKeyFrames();

   _NextThicknessAnimationUsingKeyFrames.BeginTime = _TimeSpanStarting;
   Storyboard.SetTargetName(_NextThicknessAnimationUsingKeyFrames, ToNextElement.Name);
   Storyboard.SetTargetProperty(_NextThicknessAnimationUsingKeyFrames, new PropertyPath("(FrameworkElement.Margin)"));
   _NextThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(Side ? _TicknessRight : _TicknessLeft, _KeyTimeStarting));
   _NextThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(_TicknessClient, _KeyTimeStopping));

   _StoryboardTemp.Children.Add(_NextThicknessAnimationUsingKeyFrames);

   _StoryboardTemp.Completed += (EventHandler)delegate(object sender, EventArgs e)
   {
    CurrentElement.Visibility = Visibility.Hidden;
    _Storyboard = null;
   };

   _Storyboard = _StoryboardTemp;
   BeginStoryboard(_StoryboardTemp);
  }
于 2013-01-21T21:37:58.030 回答