C# WPF隐藏在近距离?
如何在wpf中执行此操作 VB.NETC# WPF隐藏在近距离?,c#,wpf,vb.net,C#,Wpf,Vb.net,如何在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.H
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#
由于wpf的Close事件只给了我e.Cancel和no-closereason:(在wpf的默认实现中没有等价物。不过,您可以使用windows钩子来获取原因
下面的帖子详细介绍了如何做到这一点:我不确定我是否理解WinForms方法解决了什么问题 始终这样做不是更好吗:
Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
e.Cancel = True
Me.Hide()
End Sub
然后在应用程序中设置这个
Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose
这样,每当您的子窗口关闭时,您都会保留这些窗口,以便以后更快地显示,但当主窗口关闭时,您的应用程序仍会关闭(即退出、关闭等).我要感谢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();
}
多么丑陋,没有内置的东西:(这样,它将不会通过任务管理器或在windows注销时关闭。在这种情况下,您无法在退出时保存应用程序数据。我不确定我是否理解您所说的@Poma…您可以覆盖应用程序OnExit方法以处理退出时保存的数据等。如果我关闭windows应用程序,将永远不会调用OnExit方法,因为取消退出g close事件将阻止应用程序关闭。最终windows将终止此进程。这绝对不是真的。在此基本情况之外,您还有一些其他更改导致您的应用程序无法干净退出。您是否正确标记了主窗口?您的主窗口在关机时是否不可见。我的解决方案在o中工作您的应用程序可靠且干净。这样,当通过任务管理器关闭或在windows注销时,它将崩溃
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();
}