WPF/C#:程序关闭的三种模式

来自:博客园
时间:2024-06-12
阅读:

ShutdownMode枚举类型介绍

ShutdownMode是一个枚举类型,它定义了WPF应用程序的关闭方式。这个枚举类型有三个成员:

  • OnLastWindowClose:当最后一个窗口关闭或者调用System.Windows.Application.Shutdown方法时,应用程序会关闭。
  • OnMAInWindowClose:当主窗口关闭或者调用System.Windows.Application.Shutdown方法时,应用程序会关闭。
  • OnExplicitShutdown:只有当调用System.Windows.Application.Shutdown方法时,应用程序才会关闭。

整理成表格如下所示:

枚举成员 含义
OnLastWindowClose 当最后一个窗口关闭或者调用System.Windows.Application.Shutdown方法时,应用程序会关闭。
OnMainWindowClose 当主窗口关闭或者调用System.Windows.Application.Shutdown方法时,应用程序会关闭。
OnExplicitShutdown 只有当调用System.Windows.Application.Shutdown方法时,应用程序才会关闭。

实践

MainWindow的xaml如下:

<Window x:Class="ApplicationShutdown.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ApplicationShutdown"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="MainWindow_Loaded">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <Label HorizontalAlignment="Left">Shutdown Mode:</Label>
            <ComboBox HorizontalAlignment="Left" Name="shutdownModeListBox" />
        </StackPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <Label HorizontalAlignment="Left">Application Exit Code (for explicit shutdown):</Label>
            <TextBox HorizontalAlignment="Left" Name="appExitCodeTextBox">0</TextBox>
        </StackPanel>
        <Button DockPanel.Dock="Top" Click="newWindowButton_Click">New Window</Button>
        <Button DockPanel.Dock="Top" Click="explicitShutdownButton_Click">Shutdown Explicitly (Passing Exit Code)</Button>
        <Canvas />
    </DockPanel>
</Window>

MainWindow的cs如下:

using System;
using System.Windows;
using System.Windows.Controls;

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

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

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

        private void newWindowButton_Click(object sender, RoutedEventArgs e)
        {
            (new ChildWindow()).Show();
        }

        private void explicitShutdownButton_Click(object sender, RoutedEventArgs e)
        {
            var exitCode = 0;
            int.TryParse(appExitCodeTextBox.Text, out exitCode);
            Application.Current.Shutdown(exitCode);
        }
    }
}

ChildWindow的xaml如下:

<Window x:Class="ApplicationShutdown.ChildWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ApplicationShutdown"
        mc:Ignorable="d"
        Title="ChildWindow" Height="300" 
        Width="300"     
        Closing="ChildWindow_Closing" 
        Closed="ChildWindow_Closed">
    <Grid/>
</Window>

ChildWindow的cs如下:

using System;
using System.ComponentModel;
using System.Windows;

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

        private void ChildWindow_Closing(object sender, CancelEventArgs e)
        {
            Console.WriteLine(@"Closing");
            var result = MessageBox.Show("Allow Shutdown?", "Application Shutdown Sample",
                MessageBoxButton.YesNo,
                MessageBoxImage.Question);
            e.Cancel = (result == MessageBoxResult.No);
        }

        private void ChildWindow_Closed(object sender, EventArgs e)
        {
            Console.WriteLine(@"Closed");
        }
    }
}

OnLastWindowClose

当最后一个窗口关闭或者调用System.Windows.Application.Shutdown方法时,应用程序会关闭。

最后一个窗口关闭:

WPF/C#:程序关闭的三种模式

调用System.Windows.Application.Shutdown方法:

WPF/C#:程序关闭的三种模式

OnMainWindowClose

当主窗口关闭或者调用System.Windows.Application.Shutdown方法时,应用程序会关闭。

主窗口关闭:

WPF/C#:程序关闭的三种模式

或者调用System.Windows.Application.Shutdown方法关闭,与上面效果相同,这里就不重复了。

OnExplicitShutdown

只有当调用System.Windows.Application.Shutdown方法时,应用程序才会关闭。

普通关闭:

WPF/C#:程序关闭的三种模式

关闭所有窗口之后,程序并不会停止。

调用System.Windows.Application.Shutdown方法:

WPF/C#:程序关闭的三种模式

总结

本文介绍了WPF程序的三种不同的关闭模式,分别是OnLastWindowClose、OnMainWindowClose与OnExplicitShutdown。

• OnLastWindowClose:当最后一个窗口关闭或者调用System.Windows.Application.Shutdown方法时,应用程序会关闭。
• OnMainWindowClose:当主窗口关闭或者调用System.Windows.Application.Shutdown方法时,应用程序会关闭。
• OnExplicitShutdown:只有当调用System.Windows.Application.Shutdown方法时,应用程序才会关闭。

借助图解更好理解:

WPF/C#:程序关闭的三种模式

代码来源

[WPF-Samples/Application Management/ApplicationShutdown at main · microsoft/WPF-Samples (github.com)](https://github.com/microsoft/WPF-Samples/tree/main/Application Management/ApplicationShutdown)

返回顶部
顶部