简体   繁体   中英

How to properly display a splash screen

I want to display a splash screen in my app since I have to read some data on disk and customize the interface accordingly. If I didn't the effect would be that the interface is loaded and then customized, and the effect is clearly visible. So my idea is define a globla splash screen window and:

  1. In the constructor.

     WindowState = WindowState.Minimized; // <---- for the mainWindow splashScreen.Show(); 
  2. in the WindowViewBase_Loaded event

     SetInterfaceElements(); // <-------interface customization (1) splashScreen.Close(); WindowState = WindowState.Maximized; // (2) Activate(); // <------------------------to put focus on 

In the end the effect is always the same so a gap between (1) and (2).

So I thought about a refresh problem. I tried to force it with UpdateLayout but no luck. So from here another solution but always the same. Am I missing something??

What you need to do is to create a splash screen class and encapsulate all of its functions. Furthermore, you need to activate the splash screen through a thread, like this:

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
    }
}

After that your code suppose to be like that:

SplashScreenView.Show();
// all your code
SplashScreenView.Close();

This way you don't need to maximize and minimize your window.

Personally i would go with setting the Splash as the MainWindow on application initialization, doing the required loading in the loaded callback of the splash window and then opening + changing the actual MainWindow. That way you don't have to bother with threads/ui freezes.

In the mainView constructor

public MainView()
{
  SplashScreen splashScreen = new SplashScreen();
  splashScreen.Show();
  ...
}

Then

Action emptyDelegate = delegate { };
bool IsContentRendered = false;
private void WindowViewBase_Loaded(object sender, RoutedEventArgs e)
{
        SetInterfaceElements();
        Dispatcher.Invoke(emptyDelegate, DispatcherPriority.Render);<---to refresh
        IsContentRendered = true;
}

finally

private void WindowViewBase_ContentRendered(object sender, EventArgs e)
{
 if (IsContentRendered)
    {
        if (splashScreen != null)
         splashScreen.Close();
        WindowState = WindowState.Maximized;
        Activate();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM