几天前回答了问题检查WPF 关闭时执行代码
您的代码可能有问题,因为这对我来说很好。
主窗口.xaml.cs
namespace WpfApplication1
{
public partial class MainWindow : Window
{
private Foo foo;
public MainWindow()
{
InitializeComponent();
foo = new Foo();
foo.Closed += FooClosed;
foo.Show();
}
public void FooClosed(object sender, System.EventArgs e)
{
//This gets fired off
foo = null;
}
}
}
Foo.xaml
<Window x:Class="WpfApplication1.Foo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Foo" Height="300" Width="300">
<Grid>
<Button Click="Button_Click">Close</Button>
</Grid>
</Window>
Foo.xaml.cs
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Foo.xaml
/// </summary>
public partial class Foo : Window
{
public Foo()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}