How to open a child Window like a splash screen be

2020-02-06 04:49发布

I have two xaml. One is MainWindow and other is NewWindow. I want show NewWindow 5 seconds, when program is run. And after 5 seconds, I want show MainWindow.

How to change xaml in WPF?

Here is MainWindow.

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>

</Grid>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

Here is NewWindow.

<Window x:Class="WpfApplication2.NewWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="NewWindow" Height="300" Width="300">
<Grid>

</Grid>

public partial class NewWindow : Window
{
    public NewWindow()
    {
        InitializeComponent();
    }
}

标签: wpf
4条回答
我只想做你的唯一
2楼-- · 2020-02-06 05:00

1) First, we need to stop MainWindow from opening as soon as we run the Application. To do this, first remove the StartupUri="MainWindow.xaml" setting from the App.xaml file and replace it by setting the Startup property instead:

<Application x:Class="AppName.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Startup="App_Startup">

2) Then, add the handler for the Application.Startup event and launch your child (or splash screen) Window:

private SplashScreen splashScreen;

...

public void App_Startup(object sender, StartupEventArgs e)
{
    // Open your child Window here
    splashScreen = new SplashScreen();
    splashScreen.Show();
}

3) At this point, there are several different ways to go, dependent on whether you need to wait for the SplashScreen Window to do anything or not. In this particular question, the requirement is to simply open the MainWindow after 5 seconds, so we'll need a DispatcherTimer:

public void App_Startup(object sender, StartupEventArgs e)
{
    // Open your child Window here
    splashScreen = new SplashScreen();
    splashScreen.Show();
    // Initialise timer
    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = new TimeSpan(0, 5, 0);
    timer.Tick += Timer_Tick;
}

...

private void Timer_Tick(object sender, EventArgs e)
{
    splashScreen.Close();
    MainWindow mainWindow = new MainWindow();
    mainWindow.Show();
}

That's it.

查看更多
一纸荒年 Trace。
3楼-- · 2020-02-06 05:04

There are plenty ways to do this. As some people suggested i suggest too to DO NOT DO THIS if you're trying to create a splash screen, there are better ways to do that. But.. here what you asked for:

using System.ComponentModel; //Remember to add this

public partial class MainWindow : Window
{
    private BackgroundWorker waitingWorker = new BackgroundWorker();
    private NewWindow myNewWindow = new NewWindow();

    public MainWindow()
    {
        InitializeComponent();

        waitingWorker.DoWork += waitingWorker_DoWork;
        waitingWorker.RunWorkerCompleted += waitingWorker_RunWorkerCompleted;

        waitingWorker.RunWorkerAsync();
    }

    private void waitingWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        myNewWindow.Show();
        this.Close();
    }

    private void waitingWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        Thread.Sleep(5000);
    }
}

it's a simple background worker that waits for 5 seconds, then opens the NewWindow and close MainWindow. Yes, you can do it without background worker too, but Thread.Sleep(5000); will totally freeze your GUI and make your little app unresponsive, so you need another thread to wait while the main thread can keep your GUI alive. I suggest you to study at least how a background worker works.

HERE the official MSDN documentation, but google is your friend and you can find tons of tutorial and explanation about it

查看更多
一纸荒年 Trace。
4楼-- · 2020-02-06 05:17

The mainWindow should also be loaded before closing the splashScreen This way your splashscreen shows as long as it is loading.You can add additional time in the splashScreen.Close() Function.

private SplashScreen splashScreen;

private void App_Startup(object sender, StartupEventArgs e)
{
    splashScreen = new SplashScreen("SplashScreen1.png"); // Or new WPF window
    splashScreen.Show(false);
    MainWindow mainWindow = new MainWindow();
    splashScreen.Close(new TimeSpan(0, 0, 3));
    mainWindow.Show();
}
查看更多
ら.Afraid
5楼-- · 2020-02-06 05:21

Here is another way to do it:

Set Startup to "App_Startup" as shown in one of the other posts.

    <Application x:Class="AppName.App"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Startup="App_Startup">

And in App_OnStartup:

    private async void App_Startup(object sender, StartupEventArgs e)
    {
        var splash = new SplashWindow();
        splash.Show();

        await Task.Delay(5000);

        var mainWindow = new MainWindow();
        mainWindow.Show();
        splash.Close();

    }
查看更多
登录 后发表回答