简体   繁体   中英

Splashscreen does not show

I've created a SplashScreen in the following way:

  1. Created a new window with some controls such as an undeterminated progressbar for display the loading and a label

  2. Added this code to the splashscreen window:

ResizeMode="NoResize" WindowStyle="None" WindowStartupLocation="CenterScreen" Background="Blue" BorderThickness="5" BorderBrush="AliceBlue"

then in my App.xaml.cs I wrote this in the OnStartup:

SplashScreen splash = new SplashScreen();  
splash.Show();  

MainWindow main = new MainWindow();   

for (int i = 0; i < 100; i++)  
{  
    Thread.Sleep(i);  
}  

splash.Close();  

main.Show();  

now the problem's that the splashscreen does not show, seems the app is busy or something similar, why?

UPDATE:

I wrote this code as suggested:

private void _applicationInitialize(Views.SplashScreen splashWindow)
    {
        var dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();

        //Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate
        //{
        //    MainWindow = new MainWindow();
        //    MainWindow.Show();
        //});
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        MainWindow = new MainWindow();
        MainWindow.Show();
    }

but the mainwindow doesn't show

Since you are using Thread.Sleep and your splash screen is on same thread, you wont be able to see it. Possible duplicate of WPF SplashScreen with ProgressBar

I believe the problem is that the Thread.Sleep method causes the UI thread to sleep until the loop is done. Its better to use the DispatcherTimer for this sort of thing, which will spin up a background thread.

When the Tick event on the DispatcherTimer is raised, you can close the splash screen window then.

See the following link for an example of its usage: https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx

EDIT

In your edited code in the question, I realized the DispatcherTimer variable is a method-scoped variable when it should be a class-level scoped variable. The below is half psuedo-code, but should guide in how to setup the behavior:

public class Program
{
    // This is a very rough snippet of the application startup piece. Its psuedo-code.
    public void Main()
    {
        var mainWindow = new MainWindow();

        App.Run(mainWindow);
    }
}

public class MainWindow : Window
{
    private DispatcherTimer dispatcherTimer;
    private Window splashWindow;

    public MainWindow()
    {
        this.splashWindow = new SplashWindow();
        this.splashWindow.Show();

        this.dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        this.splashWindow.Close();
        this.mainWindow.Show();
    }
}

In a nutshell, the idea is to have the main window to control the display of the splash window. So I have the main window create an instance of the splash window, then use the DispatcherTimer to manage when to close it, and finally show the main window itself.

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