Hide WPF Window Until Fully Loaded

前端 未结 4 521
情话喂你
情话喂你 2021-01-02 15:10

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 eve

相关标签:
4条回答
  • 2021-01-02 15:20

    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 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
    }
    
    0 讨论(0)
  • 2021-01-02 15:32

    You can set the windows WindowState to Minimized, then handle the ContentRendered event and set the WindowState to Normal or Maximized.

    0 讨论(0)
  • 2021-01-02 15:32

    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.

    0 讨论(0)
  • 2021-01-02 15:40

    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.

    0 讨论(0)
提交回复
热议问题