1

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

Community
  • 1
  • 1
Luca
  • 918
  • 2
  • 13
  • 30
  • Is this a start-up splashscreen? Because there is a built-in template item for that. – H H Apr 18 '16 at 08:29
  • Yes it is but it has some contraints please see my comment to israel altar's answer. – Luca Apr 18 '16 at 08:31

3 Answers3

2

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.

israel altar
  • 1,768
  • 1
  • 16
  • 24
  • This is fine but if I am not getting it wrong this won't solve the problem that I have to 1. load the interface 2. customize it according to some deserialized settings. The splash screen purpose (and main miminization) is to hide that gap. – Luca Apr 18 '16 at 08:30
  • @Luca can you please explain again what is the gap, i didn't understand it from the question... – israel altar Apr 18 '16 at 08:35
  • Apologies for not making it clear I'll explain it again. the operations are 1. make a standard interface xaml based 2. deserialize data (it takes some time) 3. customize interface according to deserialized data. So in the end if I don't do something the effect is that first I see the program coming up and then...hop!... it changes according to what is deserialized data. Pretty ugly!! – Luca Apr 18 '16 at 08:38
  • @Luca just to see that i got the point, what happens is the the interface is changes in front of the user's eyes, and this is what you want to prevent? – israel altar Apr 18 '16 at 08:41
  • 1
    Yes! You got the point. It has placeholders like _TEXTBOX1, _TEXTBOX2.... and the after a flash it gets properly changed to Preferences, Users,... This is because I have a runtime language customization, user preferences customization that change the interface. – Luca Apr 18 '16 at 08:48
  • What about activating the splash screen before loading the interface, and after getting and arranging all the data close the splash and load the interface, is it possible? – israel altar Apr 18 '16 at 08:53
  • @Luca try to activate the splash screen from the App.cs, is it helps? (The chat doesn't work for me so i can't continue the discussion there) – israel altar Apr 18 '16 at 08:59
  • Sent you mail, hope you got it. BTW, delete your email address from here (you can never know who'll use it) – israel altar Apr 18 '16 at 10:55
1

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.

Dbl
  • 5,634
  • 3
  • 41
  • 66
1

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();
    }
}
Patrick
  • 3,073
  • 2
  • 22
  • 60