2016-05-06 13 views
0

私はVS2013Expressで少しWPFアプリケーションを作成していますが、少し問題があります。 あなたが見ると、3つのウィンドウ、MainWindowLatAndLongDialogTimeCitiesDialogがあります。C#WPFアプリケーションですべてのWindowsを閉じる

LatAndLongDialogTimeCitiesDialog(ボタンのクリックで)メインウィンドウから開かれています。 Closed()イベントがMainWindowで呼び出されたときに、他のすべてのウィンドウを閉じるようにします。 MainWindow.xaml.cs上のコード:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace GlobalTime_ELITE_for_WPF 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 


      UserDescText.Content = "Select a TimeCity or enter the latitude and longitude in \n" + 
           "to view the World Time there. Or, select another one of the\n" + 
           "options below to do that. Go to Help by clicking on the link\n" + 
           "on the upper-right corner of the window to view everything you\n" + 
           "can do."; 
      this.Closed += CloseOff; 
     } 

     private void OpenTimeCitiesDialog(Object Sender, EventArgs E) 
     { 
      TimeCitiesDialog ObjectReference = new TimeCitiesDialog(); 
      ObjectReference.Show(); 
     } 

     private void OpenLatAndLongDialog(Object Sender, EventArgs E) 
     { 
      LatAndLongDialog ObjectReference = new LatAndLongDialog(); 
      ObjectReference.Show(); 
     } 

     private void CloseOff(Object Sender, EventArgs E) 
     { 
      this.Close(); 

      TimeCitiesDialog tcdor = new TimeCitiesDialog(); 
      LatAndLongDialog laldor = new LatAndLongDialog(); 


     } 
    } 
} 

私は「日、すべて閉じることができますどのように?助けてください!

+0

なぜあなたは 'CloseOff()'の中で新しい 'TimeCitiesDialog'と' LatAndLongDialog'をインスタンス化していますか? – MickyD

答えて

0

使用この代わりにthis.Close()

Environment.Exit(0); 

これはあなたがそれらを開くために使用方法の範囲の外でダイアログを追跡する場合、あなたはどのこれまでに呼び出すことができます

+0

それをもっと詳しく説明してください。 –

+0

ohdon't。わかった。ありがとう。 –

+0

これはアプリケーションのすべてのウィンドウを閉じますが、OPはアプリケーションを終了するとは言いませんでした。これでアプリ全体が終了します。 OPは、これがスタートアップフォームではない状況を考え、ロジックが別のフォームからフォームを閉じるようにするだけです。参照を作成または表示するメソッドの範囲外の参照を保持する必要があります。そのテクニックに対する私の答えを見てください。 – Mikanikal

0

を閉じるためにすべてを強制しますクラス内の任意の場所から希望するダイアログのメソッド。ここでは、それらをクラス変数として持っています。インスタンス化されていますが、ボタンを押すまで表示されません。また、特定のウィンドウの「閉じる」ボタンを作成し、希望するときには.Close()メソッドを呼び出すこともできます。そうすれば自由に開いたり閉じたりすることができます。メインフォームが閉じたときには、.Close()メソッドを呼び出すこともできます。すべて閉じる

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace GlobalTime_ELITE_for_WPF 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     TimeCitiesDialog tcDialog = new TimeCitiesDialog(); 
     LatAndLongDialog lalDialog = new LatAndLongDialog(); 

     public MainWindow() 
     { 
      InitializeComponent();  

      UserDescText.Content = "Select a TimeCity or enter the latitude and longitude in \n" + 
           "to view the World Time there. Or, select another one of the\n" + 
           "options below to do that. Go to Help by clicking on the link\n" + 
           "on the upper-right corner of the window to view everything you\n" + 
           "can do."; 
      this.Closed += CloseOff; 
     } 

     private void OpenTimeCitiesDialog(Object Sender, EventArgs E) 
     {     
      tcDialog.Show(); 
     } 

     private void OpenLatAndLongDialog(Object Sender, EventArgs E) 
     {     
      lalDialog.Show(); 
     } 

     private void CloseOff(Object Sender, EventArgs E) 
     { 
      // Close the dialogs first, then allow this method 
      // to end which will finish the this.Close() process. 
      tcDialog.Close(); 
      lalDialog.Close(); 
     } 
    } 
} 
1
private void CloseAllWindows() 
     { 
     for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--) 
      App.Current.Windows[intCounter].Close(); 
     } 

現在のウィンドウを開きました。

7

WPFアプリケーションをシャットダウンする適切な方法は、Application.Current.Shutdown()です。これにより、すべての開いているWindowが閉じられ、クリーンアップコードを実行できるようにイベントが発生し、キャンセルできなくなります。 Environment.Exit()は、他のスレッドが実行中であっても、すぐにのアプリケーションを終了します。

また、Ownerを非メインのWindowに設定することを検討する必要があります。この振る舞いは、Zオーダー、最小化、最大化に関して、あなたが期待するものに近いものになるでしょう。追加ボーナスとして、所有者ウィンドウWindowが終了すると、所有ウィンドウが自動的に閉じます。

関連する問題