2009-03-12 20 views
4

どのように私は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#のWPFのCloseイベントは、ちょうど私にe.Cancelなしclosereason :(

答えて

5

を与えるよう

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

WPFの既定の実装には同等の機能はありませんが、Windowsフックを使用して理由を取得できます。

以下の投稿では、これを行う方法を詳しく説明しています。http://social.msdn.microsoft.com/forums/en-US/wpf/thread/549a4bbb-e77b-4c5a-b724-07996774c60a/

+6

はどのように醜い:( – Peter

5

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 

この方法では、いつでもあなたの子ウィンドウが閉じ、後でより速く表示するための周りにそれらを保つが、あなたのアプリはまだshutsdownと、メインウィンドウが閉じる(すなわち、終了、シャットダウンなど)。

+0

この方法で構築されたものは何も存在しないことには、タスクマネージャを通じ閉じまたはWindowsログオフではありません。また、あなたがアプリケーションデータを保存することはできません。この場合には。 – Poma

+0

あなたが何を言っているのか分かりません...終了時にデータを保存するために、アプリケーションOnExitメソッドをオーバーライドすることができます。 –

+0

Windowsアプリケーションをシャットダウンすると、OnExitメソッドは決して呼び出されません – Poma

5

私は彼のヒントとして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(); 
} 
+2

この方法では、タスクマネージャまたはウィンドウログオフで閉じるとクラッシュします – Poma

関連する問題