2012-10-02 3 views
1

트레이 응용 프로그램에서 WPF 윈도우를 열려고합니다. 다음 코드를 사용하여 창을 엽니 다.WPF UI가 닫힐 때의 알림

 if (gui == null) 
     { 
      gui = new App(); 
      gui.MainWindow = new mainWindow(); 
      gui.InitializeComponent(); 
      IsUIOpen = true; 
     } 
     else if (!IsUIOpen) 
     { 
      gui.InitializeComponent(); 
      gui.MainWindow.Show(); 
      gui.MainWindow = new mainWindow(); 
      IsUIOpen = true; 
     } 

UI가 리소스 사전을 사용하기 때문에 App 수준에서 실행해야합니다. 문제는, 사용자가 창을 닫을 때 코드를 실행해야하지만, 이벤트 처리기 중 아무 것도 나를 알리는 것 같지 않습니다.

나는 다음과 같은 시도 :

gui.Exit += new System.Windows.ExitEventHandler(settings_FormClosed); 
gui.MainWindow.Closed += new EventHandler(settings_FormClosed); 

가 나는 또한 gui.Deactivated, gui.SessionEnding, gui.MainWindow.Closing, gui.MainWindow.Deactivated, 그리고 아마 다른 사람을 시도했습니다. 나는 그래서 가까이 않습니다, 응용 프로그램은 정적 실현

private void Cancel_Click(object sender, RoutedEventArgs e) 
    { 
     presenter.Close(); 
     this.Close(); 
    } 

하지만, 이러한 이벤트 핸들러 중 하나에 저를 연결해야합니다

사용자가 창을 닫습니다

,이 코드는 Shell.xaml에서 호출 닫는 이벤트. 이 유용합니다 경우에는 다음과 같이

이 흐름은 다음과 같습니다 TrayApp.cs -> App.xaml -> 어떤 제안을 감상 할 수있다 Shell.xaml

. 미리 감사드립니다.

답변

0

조쉬가 올바른 해결책을 제시 할 수있었습니다. 그의 대답 here을 볼 수 있습니다.

기본적으로 WPF를 별도의 프로세스로 시작한 다음 MyProcess.WaitForEnd() 호출을 사용해야했습니다. 나는 그것을 스레드에 추가하여 트레이를 차단하지 않을 것입니다. 코드는 다음과 같습니다 :

Process myProcess = new Process(); 
myProcess.StartInfo.UseShellExecute = false; 
myProcess.StartInfo.FileName = "C:\\mysettingsapp\\mysettingsapp.exe"; // replace with path to your settings app 
myProcess.StartInfo.CreateNoWindow = false; 
myProcess.Start(); 
// the process is started, now wait for it to finish 
myProcess.WaitForExit(); // use WaitForExit(int) to establish a timeout 
1

Closing 이벤트를 시도해야합니다. This article은 WPF가 실제로 창을 닫을 때 유용한 정보를 제공합니다.

+0

응답 해 주셔서 감사합니다, Damian,하지만 Closing 이벤트를 시도했습니다. UI를 닫을 때 실행되지 않는 것 같습니다. 이상하게도 전체 애플리케이션을 닫을 때도 실행되지 않습니다. – Tim

+0

흠,이게 도움이 되니? http://stackoverflow.com/questions/8908276/why-does-unloaded-event-of-window-do-not-fire-in-wpf http://stackoverflow.com/questions/3183729/why-isnt-my -wpf-closing-event-for-system-closes입니다. –

관련 문제