我什么时候能处理的了IDisposable WPF控件如的WindowsFormsHost? [英] When can I dispose an IDisposable WPF control e.g. WindowsFormsHost?

查看:446
本文介绍了我什么时候能处理的了IDisposable WPF控件如的WindowsFormsHost?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF控件的WindowsFormsHost继承自IDisposable。

The WPF control WindowsFormsHost inherits from IDisposable.

如果我有一个包含一些什么事件或方法,我可以用在关闭期间调用IDispose上述控制的复杂WPF可视化树?

If I have a complex WPF visual tree containing some of the above controls what event or method can I use to call IDispose during shutdown?

推荐答案

从托德的回答大厦我想出了由一个窗口承办,并希望是保证处置时,该窗口关闭任何WPF控件这个通用的解决方案。

Building from Todd's answer I came up with this generic solution for any WPF control that is hosted by a Window and want's to guarantee disposal when that window is closed.

(显然,如果你能避免IDisposable的继承做,但有时你只是不能)

(Obviously if you can avoid inheriting from IDisposable do, but sometimes you just can't)

当在层次结构中的第一个父窗口关闭调用Dispose

Dispose is called when the the first parent window in the hierarchy is closed.

(可能的改进 - 改变事件处理使用弱势格局)

(Possible improvement - change the event handling to use the weak pattern)

public partial class MyCustomControl : IDisposable
    {

        public MyCustomControl() {
            InitializeComponent();

            Loaded += delegate(object sender, RoutedEventArgs e) {
                System.Windows.Window parent_window = Window.GetWindow(this);
                if (parent_window != null) {
                    parent_window.Closed += delegate(object sender2, EventArgs e2) {
                        Dispose();
                    };
                }
            };

            ...

        }

        ...
    }

这篇关于我什么时候能处理的了IDisposable WPF控件如的WindowsFormsHost?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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