如何让WPF的故事板在启动后的预定义时间停止/暂停?例如,开始后20秒?
答案 0 :(得分: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);
}