views:

836

answers:

3

For my WPF application, I am storing several user settings like window position, window state, and whether or not to display a welcome dialog. The problem is that while everything is loading up, I see a lot of flashing and flickering as the windows are loaded in, and then more flickering when the window is maximized after reading in the settings.

I am already using the built-in WPF PNG splash screen functionality, but is there a way to completely hide the rendering of all windows until everything is fully loaded in?

+4  A: 

There are functions , BeginInit and EndInit, if you change properties inside these functions like..

BeginInit();
...
... // Do your code Initialization here...
...
EndInit();

then your window will not render until the EndInit() is called, it will not flicker.

Akash Kava
+4  A: 

Edit the Application.xaml, remove the StartUpUri, instead set the StartUp event handler. In Application.xaml.cs, edit the startup event handler to display the splashscreen, load your resources, create everything, then create the main window and show it.

<Application
    ...
    StartUp="OnStartUp"
    />

And:

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

    if (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)
        splashScreen.Close();
    mainWindow.Show();
}
Danny Varod
Excellent, thank you!
Ben McIntosh
A: 

When does this loading occur? Code executed in the main Window's constructor should execute before the window is shown; if you load any required resources there, you should not see any flickering.

Eamon Nerbonne