Window Loaded event

image_pdfimage_print


   
  


<Window x:Class="ApplicationShutdownSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Main Window" Height="300" Width="300" Loaded="MainWindow_Loaded">
  <StackPanel>
      <Label HorizontalAlignment="Left">Shutdown Mode:</Label>
      <ComboBox HorizontalAlignment="Left" Name="shutdownModeListBox"></ComboBox>
      <Button Click="explicitShutdownButton_Click">Shutdown Explicitly (Passing Exit Code)</Button>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ApplicationShutdownSample
{
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.shutdownModeListBox.Items.Add("OnLastWindowClose");
            this.shutdownModeListBox.Items.Add("OnExplicitShutdown");
            this.shutdownModeListBox.Items.Add("OnMainWindowClose");
            this.shutdownModeListBox.SelectedValue = "OnLastWindowClose";
            this.shutdownModeListBox.SelectionChanged += new SelectionChangedEventHandler(shutdownModeListBox_SelectionChanged);
            Application.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
        }

        void shutdownModeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Application.Current.ShutdownMode = (ShutdownMode)Enum.Parse(typeof(ShutdownMode), this.shutdownModeListBox.SelectedValue.ToString());
        }


        void explicitShutdownButton_Click(object sender, RoutedEventArgs e)
        {
            int exitCode = 0;
            Application.Current.Shutdown(exitCode);
        }
    }
}