tags:

views:

193

answers:

5

I'm creating a wpf application in c#, I know to close/open a window you have to use the .Close() and .Show() methods but for some reason the home screen, the first window that appears when I launch the application, won't close.

        Home window1 = new Home();
        window1.Close();
        Name window2 = new Name();
        window2.Show();

Window2 appears, but window1 won't close. What's the problem.

+1  A: 

Presumably because if you close the window you'll close the application.

If you just want to hide the main window use the window.Hide() method.

This from the help on Window.Close:

A Window can be closed using one of several, well-known, system-provided mechanisms located in its title bar, including:

ALT+F4.

System menu | Close.

Close button.

A Window can also be closed using one of several well-known mechanisms within the client area that are provided by developers, including:

File | Exit on a main window.

File | Close or a Close button on a child window.

UPDATE

Tormod Fjeldskår has a good point in his answer. I assumed that the code was given as an example rather than being what was actually being used.

ChrisF
+1  A: 

Where is your code for showing window1? If you show your home window somewhere else in your code, you need to use that reference in order to close it. Making a new Home object and calling its Close method will not close a window shown using another Home object.

Tormod Fjeldskår
I've noticed that when calling the close method, it doesn't close the previous window. For example, if I have window1 open, and open window2, I can't close window1 but I CAN close window2. Then when I have window2 open and open window3, i can't close window1 or window2 but I can close window3. And it goes on, only the latest window can be closed, anything prior cannot. How can I close previous windows.
Jake
Where do you store the references to your window objects?
Tormod Fjeldskår
A: 

If close the window not necessary, just hide them.

mykhaylo
A: 

Or you could have Window2 be the main window (you can change this in app.xaml in the StartUpUri property) and either have Window2 show and close Window1 or not show Window1 at all.

<Application x:Class="Invitrogen.TheGadget.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window2.xaml">
</Application>
Carlo
A: 

This is a bug in WPF. Window.Close will fail silently if the SourceInitialized event has not yet occurred. Subsequent calls to Window.Close will also fail.

https://connect.microsoft.com/WPF/feedback/ViewFeedback.aspx?FeedbackID=299100

For a workaround, add this to your Window:

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);

    // check if we've already been closed
    if (m_bClosed)
    {
        // close the window now
        Close();
    }
}

protected override void OnClosing(CancelEventArgs e)
{
    base.OnClosing(e);

    // make sure close wasn't cancelled
    if (!e.Cancel)
    {
        // mark window as closed
        m_bClosed = true;

        // if our source isn't initialized yet, Close won't actually work,
        // so we cancel this close and rely on SourceInitialized to close
        // the window
        if (new WindowInteropHelper(this).Handle == IntPtr.Zero)
            e.Cancel = true;
    }
}

bool m_bClosed;
Ed Ball