如何关闭主窗口

时间:2021-03-19 17:04:37

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.

我在c#中创建一个wpf应用程序,我知道要关闭/打开一个窗口,你必须使用.Close()和.Show()方法,但出于某种原因主屏幕,我启动时出现的第一个窗口申请,不会关闭。

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

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

出现Window2,但window1不会关闭。有什么问题。

5 个解决方案

#1


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.

显示window1的代码在哪里?如果您在代码中的其他位置显示主窗口,则需要使用该引用以关闭它。创建一个新的Home对象并调用其Close方法不会关闭使用另一个Home对象显示的窗口。

#2


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.

如果您只想隐藏主窗口,请使用window.Hide()方法。

This from the help on Window.Close:

这来自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.

TormodFjeldskår在他的回答中有一个很好的观点。我假设代码是作为示例给出的,而不是实际使用的代码。

#3


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.

这是WPF中的一个错误。如果尚未发生SourceInitialized事件,Window.Close将无提示失败。对Window.Close的后续调用也将失败。

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

For a workaround, add this to your Window:

要解决此问题,请将其添加到您的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;

#4


If close the window not necessary, just hide them.

如果不需要关闭窗口,只需隐藏它们即可。

#5


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.

或者你可以将Window2作为主窗口(你可以在StartUpUri属性中的app.xaml中更改它),或者让Window2显示并关闭Window1或者根本不显示Window1。

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

#1


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.

显示window1的代码在哪里?如果您在代码中的其他位置显示主窗口,则需要使用该引用以关闭它。创建一个新的Home对象并调用其Close方法不会关闭使用另一个Home对象显示的窗口。

#2


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.

如果您只想隐藏主窗口,请使用window.Hide()方法。

This from the help on Window.Close:

这来自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.

TormodFjeldskår在他的回答中有一个很好的观点。我假设代码是作为示例给出的,而不是实际使用的代码。

#3


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.

这是WPF中的一个错误。如果尚未发生SourceInitialized事件,Window.Close将无提示失败。对Window.Close的后续调用也将失败。

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

For a workaround, add this to your Window:

要解决此问题,请将其添加到您的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;

#4


If close the window not necessary, just hide them.

如果不需要关闭窗口,只需隐藏它们即可。

#5


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.

或者你可以将Window2作为主窗口(你可以在StartUpUri属性中的app.xaml中更改它),或者让Window2显示并关闭Window1或者根本不显示Window1。

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