2013-08-14 5 views
4

WPF에서 작업 중입니다. 응용 프로그램이 시작되면서 열리는 Mainwindow가 있습니다. 이 창에는 두 개의 버튼이 있습니다. 각각 새로운 창이 열립니다. 예 : 추가 및 업데이트 버튼이 있습니다. 추가 버튼은 클릭 이벤트 호출시 Add-Item 창을 열고 비슷하게 업데이트하면 "Update-Item"창이 열립니다. 메인 윈도우를 닫으면 "Add-Item"과 "Update-Item"창이 열립니다. 나는 Mainwindow를 닫으면 다른 두 개의 창도 닫아야한다.Mainwindow가 닫힐 때 모든 창을 닫는 방법

app.current.shutdown 

app.current.shutdown 주로 사용된다. 내 질문은 : 어디에 내 프로그램, mainwindow 또는 App.config에서이 코드 줄을 게시해야합니다. 응답에 이벤트 나 기능을 호출해야합니까?

답변

5

기본 창의 인스턴스에 Application.MainWindow을 설정하고 Application.ShutdownModeOnMainWindowClose인지 확인하십시오.

또한 전체 응용 프로그램을 종료하지 않으려면 MainWindow의 자식 창을 Owner으로 만드십시오. (다른 부작용이 있습니다)

+0

? @HBB –

+0

@ ZoyaSheikh : 정말로 중요하지는 않지만,'App' 클래스의'Application.OnStartup'에서 할 것입니다. (도랑'StartupUri'는 수동으로 창을 창조하고, 그것을 할당한다) –

0

: 그런 식으로, 메인 창에서 수행되는 윈도우 동작은 또한 다른 창을 모두 수행됩니다 약간 예제 (모범 사례) 코드으로 진행합니다.

조쉬 스미스App.xaml.cs를이 같아야하는 방법을 보여줍니다 것입니다.

namespace MyApplication 
{ 
    /// <summary> 
    /// Interaction logic for App.xaml 
    /// </summary> 
    public partial class App : Application 
    { 
     static App() 
     { 
      // Ensure the current culture passed into bindings is the OS culture. 
      // By default, WPF uses en-US as the culture, regardless of the system settings. 
      // 
      FrameworkElement.LanguageProperty.OverrideMetadata(
       typeof(FrameworkElement), 
       new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag))); 
     } 

     protected override void OnStartup(StartupEventArgs e) 
     { 
      base.OnStartup(e); 

      var window = new MainWindow(); 

      // To ensure all the other Views of a type Window get closed properly. 
      ShutdownMode = ShutdownMode.OnMainWindowClose; 

      // Create the ViewModel which the main window binds. 
      var viewModel = new MainWindowViewModel(); 

      // When the ViewModel asks to be closed, 
      // close the window. 
      EventHandler handler = null; 
      handler = delegate 
      { 
       viewModel.RequestClose -= handler; 
       window.Close(); 
      }; 
      viewModel.RequestClose += handler; 

      // Allow all controls in the window to bind to the ViewModel by 
      // setting the DataContext, which propagates down the element tree. 
      window.DataContext = viewModel; 
      window.Show(); 
     } 
    } 
} 

다시 하루의 끝에서, 당신이 당신의 MVVM 응용 프로그램을 레이아웃 할 것입니다 방법에 달려 있습니다.

0

당신은 또한 App.xaml 파일은 ShutdownMode를 설정할 수 있습니다 :이 설정해야

<Application x:Class="WpfApp1.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApp1" 
    StartupUri="MainWindow.xaml" 
    ShutdownMode="OnMainWindowClose"> 
    <Application.Resources> 
    </Application.Resources> 
</Application>