隐藏WPF窗口直到完全加载

Ben*_*osh 8 c# wpf splash-screen loading

对于我的WPF应用程序,我存储了几个用户设置,如窗口位置,窗口状态,以及是否显示欢迎对话框.问题是,虽然一切都在加载,但是当窗口被加载时我看到很多闪烁和闪烁,然后在读取设置后窗口最大化时更加闪烁.

我已经在使用内置的WPF PNG启动画面功能,但有没有办法完全隐藏所有窗口的渲染,直到所有内容都完全加载?

Dan*_*rod 10

编辑Application.xaml,删除StartUpUri,而不是设置StartUp事件处理程序.在Application.xaml.cs中,编辑启动事件处理程序以显示启动画面,加载资源,创建所有内容,然后创建主窗口并显示它.

<Application
    ...
    StartUp="OnStartUp"
    />
Run Code Online (Sandbox Code Playgroud)

和:

private void OnStartUp(Object sender, StartupEventArgs e)
{
    var settings = LoadSettingsFrom... // Call your implementation of load user settings

    // Example only, in real app do this if's section on a different thread
    if (settings.doShowSplashScreen)
    {
        var splashScreen = new SplashScreen();
        splashScreen.Show();
    }

    // Load and create stuff (resources, databases, main classes, ...)

    var mainWindow = new mainWindow();
    mainWindow.ApplySettings(settings); // Call your implementation of apply settings

    if (doShowSplashScreen)
    {
        // send close signal to splash screen's thread
    }

    mainWindow.Show(); // Show the main window
}
Run Code Online (Sandbox Code Playgroud)


Sha*_*awn 5

您可以将窗口的 WindowState 设置为“最小化”,然后处理 ContentRendered 事件并将 WindowState 设置为“正常”或“最大化”。