1

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();
    }
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
user3755149
  • 21
  • 1
  • 4
  • I.e. you want to show a splash screen? If yes, [MSDN](http://msdn.microsoft.com/en-us/library/cc656886%28v=vs.110%29.aspx) contains an explanation how to do that. If not, please give a more detailed question. –  Jun 19 '14 at 06:19
  • Use a timer and open `NewWindow`. – Loetn Jun 19 '14 at 06:19
  • could you post some code what you've tried. as there are several ways to do the same. – pushpraj Jun 19 '14 at 06:22
  • is it a splash screen? – Rang Jun 19 '14 at 06:27
  • I make NewWindow with Expression Blend. Because use to loading screen. – user3755149 Jun 19 '14 at 06:28
  • you want to change the xaml of the first window or want to close the first one and open the second after 5 second? – pushpraj Jun 19 '14 at 06:40
  • I want to close the first one and open the second after 5 second. – user3755149 Jun 19 '14 at 06:44
  • Doing a splash screen that way has problems... Firstly it doesn't show until _after_ your program is ready to run, second it then slows down someone wanting to use your program by making them wait for no reason. The way shown in the linked MSDN article avoids both of those issues by letting the framework show the splash screen immediately and get rid of it when it's no longer needed – Basic Jun 19 '14 at 06:44

4 Answers4

5

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.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • To make this work the timer should be stopped in the Timer_Tick method. Also the splashScreen should be closed after showing the main window. Otherwise a nice solution. Thanks :-) – AH. Nov 04 '14 at 07:49
2

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

HypeZ
  • 4,017
  • 3
  • 19
  • 34
0

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();

    }
AH.
  • 2,913
  • 1
  • 28
  • 26
0

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();
}