4

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)
{
}

When someone presses a button inside the foo Window I call this.Close() but my FooClosed doesn't seem to be called.

Am I incorrectly signing up for the event?

Update

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?

Ternary
  • 2,401
  • 3
  • 31
  • 54

1 Answers1

11

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

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();
        }
    }
}
Community
  • 1
  • 1
Dan P
  • 1,939
  • 3
  • 17
  • 30
  • 1
    The difference between what I'm doing and that link you provided is I am trying to handle a Window being closed from outside the Window itself (i.e. I'm not doing this.closed) I'm handling it from a parent window. – Ternary Apr 05 '12 at 18:08
  • @Ternary - Are you sure the window "foo" isn't getting destroyed before your mapped event has the chance to be called? Maybe you should hook the Window.Closing() to see if that's the case. – Alain Apr 05 '12 at 18:09
  • I'll try that. But what's the point of Closed if there's a chance you'll never be notified? – Ternary Apr 05 '12 at 18:10
  • That's a really good point, but the documentation is sparse. My first impression would also be to think that that wouldn't be the case. – Alain Apr 05 '12 at 18:11
  • Ohh and thanks for the new syntax on foo.Closed += FooClosed; as opposed to the new EventHandler method. I was never aware of that shortcut. I always just use += and hit tab tab and the code is auto generated for me that way. – Dan P Apr 05 '12 at 18:24
  • +1 for creating a sample application that proves it works. The problem is somewhere else. – Alain Apr 05 '12 at 18:35
  • Yeah I was going to suggest modifying the above code until you can figure out what is causing your code to not work. If you can find out what is causing the Closed event handler to not work we can probably figure out a solution around it. – Dan P Apr 05 '12 at 18:38