需求:需要在许多子窗体中关闭整个程序(WPF)
winform实现:Application.Exit();
WPF实现:
App.xaml文件:
<Application x:Class="pc.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Windows1.xaml" ShutdownMode="OnExplicitShutdown">
Windows1.xaml文件(部分):
<Button Margin="37,0,15,15" Style="{DynamicResource btn_Exit}" Content="Button" Grid.Column="2" Grid.Row="3" Height="41" VerticalAlignment="Bottom" Width="100" x:Name="btn_Exits" Click="btn_Exits_Click" />
Windows1.xaml.cs文件(部分):
private void btn_Exits_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
//如果在子线程中使用,会报线程错误。
//改用Environment.Exit(0) 会更稳妥些
//Environment.Exit(0)
}