WPF窗口关闭事件用法 [英] WPF Window Closed Event Usage

查看:3473
本文介绍了WPF窗口关闭事件用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口类(如公共部分类Foo:窗口),当我创建窗口我报名参加关闭事件本身。

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

当有人按下窗口我称之为内按钮 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.

我是不正确的报名参加事件?

Am I incorrectly signing up for the event?

更新

顺便说一句,所有我想要做到的是的知道的当已经关闭,所以我可以设置参考回。有没有更好的方式来实现这一目标。

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?

推荐答案

问题得到回答,前几天检查的Execute代码

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

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆