WPF Window Closed Event Usage

后端 未结 1 1318
星月不相逢
星月不相逢 2021-01-14 04:16

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.

1条回答
  •  梦谈多话
    2021-01-14 05:03

    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

    
        
            
        
    
    

    Foo.xaml.cs

    namespace WpfApplication1
    {
        /// 
        /// Interaction logic for Foo.xaml
        /// 
        public partial class Foo : Window
        {
            public Foo()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题