【发布时间】:2014-06-22 20:32:00
【问题描述】:
以下情况:我得到了一个闪屏,它必须在 2 秒内淡出(从透明到不透明)。当窗口很好地淡入时,程序应该等到它完全可见(故事板完成),然后它应该继续做它的东西。当窗口淡入时,用户应该已经看到了控件的动画(当然,阻止 UI 线程不是一种选择)。对于动画,我指的是快乐旋转的加载圈。
在我找到了如何使用 WPF 的情节提要淡入和淡出窗口的一个很好的变体之后,我尝试使用 EventWaitHandle 来完成此操作。由于我的初始化例程已经异步运行,我可以使用它。这阻止了工作线程并在 Splashscreen 完全可见之前停止了我的应用程序执行初始化工作。但不知何故,这在一段时间后就被打破了,这似乎不是最好的解决方案。
这是我目前的做法:
public async Task Appear(double time)
{
Core.IDE.GetGUICore().GetUIDispatcher().Invoke(() =>
{
this.Opacity = 0;
this.Show();
_fadeInStoryboard = new Storyboard();
_fadeInStoryboard.Completed += this.FadeInAnimation;
DoubleAnimation fadeInAnimation = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(time)));
Storyboard.SetTarget(fadeInAnimation, this);
Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(OpacityProperty));
_fadeInStoryboard.Children.Add(fadeInAnimation);
});
_currentHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
Core.IDE.GetGUICore()
.GetUIDispatcher()
.InvokeAsync(this._fadeInStoryboard.Begin, DispatcherPriority.Render);
_currentHandle.WaitOne();
}
/// <summary>
/// Hides the SplashScreen with a fade-out animation.
/// </summary>
/// <param name="time">The fade-out time in seconds.</param>
public void Disappear(double time)
{
Core.IDE.GetGUICore().GetUIDispatcher().Invoke(() =>
{
_fadeOutStoryboard = new Storyboard();
_fadeOutStoryboard.Completed += this.FadeOutAnimation;
DoubleAnimation fadeOutAnimation = new DoubleAnimation(1.0, 0.0,
new Duration(TimeSpan.FromSeconds(time)));
Storyboard.SetTarget(fadeOutAnimation, this);
Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(OpacityProperty));
_fadeOutStoryboard.Children.Add(fadeOutAnimation);
});
Core.IDE.GetGUICore()
.GetUIDispatcher()
.BeginInvoke(new Action(_fadeOutStoryboard.Begin), DispatcherPriority.Render, null);
}
private void FadeInAnimation(object sender, EventArgs e)
{
_currentHandle.Set();
}
private void FadeOutAnimation(object sender, EventArgs e)
{
this.Hide();
}
这是正确的方法吗?有更好的解决方案吗?任何想法为什么它被打破? 顺便说一句,破碎的意思是应用程序在窗口淡入时继续执行其初始化工作,最终以动画结束,该动画一直运行到它可能处于 30% 的可见度,然后因为主窗口已经出现而淡出。
提前致谢
【问题讨论】:
标签: c# wpf multithreading storyboard splash-screen