C# WPF 关闭时隐藏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/638942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:20:52  来源:igfitidea点击:

WPF Hide on Close?

c#wpfvb.net

提问by Peter

How do i do this in wpf

我如何在 wpf 中执行此操作

VB.NET

网络

   Private Sub FrmSettings_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        e.Cancel = (e.CloseReason = Forms.CloseReason.UserClosing)
        Me.Hide()
    End Sub

c#

C#

private void FrmSettings_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
    e.Cancel = (e.CloseReason == Forms.CloseReason.UserClosing);
    this.Hide();
}

as wpf's Close event just gives me e.Cancel and no closereason :(

因为 wpf 的 Close 事件只是给了我 e.Cancel 而没有 closereason :(

采纳答案by JaredPar

There is not an equivalent in the default implementation of WPF. You can use a windows hook to get the reason though.

WPF 的默认实现中没有等效项。不过,您可以使用 Windows 挂钩来获取原因。

The following post details how to do this: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/549a4bbb-e77b-4c5a-b724-07996774c60a/

以下帖子详细说明了如何执行此操作:http: //social.msdn.microsoft.com/forums/en-US/wpf/thread/549a4bbb-e77b-4c5a-b724-07996774c60a/

回答by Bob King

I'm not sure I understand what the WinForms approach solves.

我不确定我是否理解 WinForms 方法解决的问题。

Isn't it better to always do this:

总是这样做不是更好吗:

Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
    e.Cancel = True
    Me.Hide()
End Sub

And then set this in your Application?

然后在您的应用程序中设置它?

Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose

This way, whenever your child windows close, you keep them around for faster display later, but your app still shutsdown when the main window closes (i.e. Exit, Shutdown, etc).

这样,每当您的子窗口关闭时,您将它们保留在周围以便稍后更快地显示,但是当主窗口关闭(即退出、关闭等)时,您的应用程序仍会关闭。

回答by DK.

I would like to thank Bob King for his hint and make an addition to his code as C# WPF. It works for me. My application is a tray icon by type. In a WPF XAML form code behind:

我要感谢 Bob King 的提示,并将他的代码添加为 C# WPF。这个对我有用。我的应用程序是按类型分类的托盘图标。在 WPF XAML 表单代码后面:

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

    Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
}

private bool m_isExplicitClose = false;// Indicate if it is an explicit form close request from the user.

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)

{

    base.OnClosing(e);

    if (m_isExplicitClose == false)//NOT a user close request? ... then hide
    {
        e.Cancel = true;
        this.Hide();
    }

}

private void OnTaskBarMenuItemExitClick(object sender, RoutedEventArgs e)

{            
    m_isExplicitClose = true;//Set this to unclock the Minimize on close 

    this.Close();
}