[英]How to properly display a splash screen
我想在我的应用程序中显示启动屏幕,因为我必须读取磁盘上的一些数据并相应地自定义界面。 如果不是这样的话,效果将是先加载接口然后对其进行自定义,并且效果清晰可见。 因此,我的想法是定义一个globla初始屏幕窗口并:
在构造函数中。
WindowState = WindowState.Minimized; // <---- for the mainWindow splashScreen.Show();
在WindowViewBase_Loaded事件中
SetInterfaceElements(); // <-------interface customization (1) splashScreen.Close(); WindowState = WindowState.Maximized; // (2) Activate(); // <------------------------to put focus on
最后,效果始终是相同的,因此(1)和(2)之间存在差距。
所以我想到了一个刷新问题。 我试图用UpdateLayout强制它,但是没有运气。 所以从这里开始,另一个解决方案却始终是相同的。 我错过了什么吗?
您需要做的是创建一个启动画面类并封装其所有功能。 此外,您需要通过一个线程激活启动屏幕,如下所示:
public static class SplashScreenView
{
public static Show()
{
Thread thread = new Thread(() =>
{
splashScreenView = new SplashScreenView();
....
}
// you code
thread.Start();
}
public static Close()
{
// close splash screen code
}
}
之后,您的代码应该像这样:
SplashScreenView.Show();
// all your code
SplashScreenView.Close();
这样,您无需最大化和最小化窗口。
我个人将在应用程序初始化时将Splash设置为MainWindow,在启动窗口的已加载回调中进行所需的加载,然后打开+更改实际的MainWindow。 这样,您就不必担心线程/ UI冻结。
在mainView构造函数中
public MainView()
{
SplashScreen splashScreen = new SplashScreen();
splashScreen.Show();
...
}
然后
Action emptyDelegate = delegate { };
bool IsContentRendered = false;
private void WindowViewBase_Loaded(object sender, RoutedEventArgs e)
{
SetInterfaceElements();
Dispatcher.Invoke(emptyDelegate, DispatcherPriority.Render);<---to refresh
IsContentRendered = true;
}
最后
private void WindowViewBase_ContentRendered(object sender, EventArgs e)
{
if (IsContentRendered)
{
if (splashScreen != null)
splashScreen.Close();
WindowState = WindowState.Maximized;
Activate();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.