WPF窗口关闭事件使用 [英] WPF Window Closed Event Usage
问题描述
我有一个窗口
类(例如 public partial class Foo:Window
),当我创建窗口我注册了已关闭的
事件。
I have a Window
class (e.g. public partial class Foo : Window
), and when I create the window I sign up for the Closed
event as such.
foo = new Foo();
foo.Closed += FooClosed;
public void FooClosed(object sender, System.EventArgs e)
{
}
当有人按下 foo
窗口内的按钮我调用 this.Close()
但我的 FooClosed
似乎没有被调用。
When someone presses a button inside the foo
Window I call this.Close()
but my FooClosed
doesn't seem to be called.
我错误地注册了事件?
更新
顺便说一句,我正在尝试当 foo
已经关闭时,知道,所以我可以将引用设置为 null
。有没有更好的方法来实现?
By the way, all I'm trying to accomplish is know when foo
has been closed so I can set the reference back to null
. Is there a better way to accomplish that?
推荐答案
问题回答了几天前,检查 WPF关闭时执行代码
Question was answered a few days ago check Execute code when a WPF closes
你可能会有一些事情发生在你的代码,因为这对我来说很好。
You may have something going on with your code because this works just fine for me.
MainWindow.xaml.cs
MainWindow.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;
}
}
}
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
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();
}
}
}
这篇关于WPF窗口关闭事件使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!