简体   繁体   English

我什么时候可以处理IDisposable WPF控件,例如WindowsFormsHost?

[英]When can I dispose an IDisposable WPF control e.g. WindowsFormsHost?

The WPF control WindowsFormsHost inherits from IDisposable. WPF控件WindowsFormsHost继承自IDisposable。

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可视树,我可以使用哪些事件或方法在关机期间调用IDispose?

In the case of application shutdown there is nothing you need to do to properly dispose of the WindowsFormsHost. 在应用程序关闭的情况下,您无需执行任何操作来正确处理WindowsFormsHost。 Since it derives from HwndHost disposing is handled when the Dispatcher is shutdown. 由于它派生自HwndHost,因此在Dispatcher关闭时会处理。 If you use Reflector you will see that when HwndHost is initialized it creates a WeakEventDispatcherShutdown. 如果使用Reflector,您将看到在初始化HwndHost时它会创建一个WeakEventDispatcherShutdown。

If you are using it in a dialog the best I can suggest is to override OnClosed and dispose of your Host then, otherwise the HwndHost will hang around until until the Dispatcher is shutdown. 如果你在一个对话框中使用它,我建议的最好是覆盖OnClosed然后处理你的主机,否则HwndHost会一直闲置直到Dispatcher关闭。

public partial class Dialog : Window
{
    public Dialog()
    {
        InitializeComponent();
    }

    protected override void OnClosed(EventArgs e)
    {
        if (host != null)
            host.Dispose();

        base.OnClosed(e);
    }
}

A simple way to test when dispose gets called is to derive a custom class from WindowsFormsHost and play around with different situations. 测试dispose何时被调用的一种简单方法是从WindowsFormsHost派生自定义类并在不同情况下进行游戏。 Put a break point in dispose and see when it gets called. 在处理中放置一个断点并查看它何时被调用。

public class CustomWindowsFormsHost : WindowsFormsHost
{
    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
    }
}

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. 根据Todd的回答,我想出了一个由Window托管的任何WPF控件的通用解决方案,并希望在窗口关闭时保证处理。

(Obviously if you can avoid inheriting from IDisposable do, but sometimes you just can't) (显然如果你可以避免从IDisposable继承,但有时你不能)

Dispose is called when the the first parent window in the hierarchy is closed. 当关闭层次结构中的第一个父窗口时,将调用Dispose。

(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();
                    };
                }
            };

            ...

        }

        ...
    }

关闭表单时不需要处理控件,如果控件位于表单的可视树中(作为表单的子表单或表单中的其他控件),API将自动为您执行操作

WPF Controls don't implement the IDisposable interface, because they have nothing to dispose (No handles to clean up, no unmanaged memory to release). WPF控件不实现IDisposable接口,因为它们没有任何可处置的内容 (没有要清理的句柄,没有要释放的非托管内存)。 All you need to do is make sure that you don't have any references to the controls and the GC will clean them. 您需要做的就是确保您没有对控件的任何引用,GC将清除它们。

Therefore WPF employs weak event patterns to ensure that controls can be garbage collected. 因此,WPF使用弱事件模式来确保可以对控件进行垃圾回收。 This is the pattern you need to implement to ensure clean-up, not IDisposable. 这是您需要实现的模式,以确保清理,而不是IDisposable。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在wpf控件的名称中使用索引(例如TextBox(i))来减少代码重复? - Is it possible to use index in wpf control's name (e.g. TextBox(i)) to lessen code duplication? 在参数中声明IDisposable时如何处理它? - How can i dispose IDisposable when it is declared in a parameter? WPF Xaml 网格 - 我可以从末尾开始对列进行编号(例如 Grid.Column=“-1”) - WPF Xaml Grid - can I number columns starting from the end (e.g. Grid.Column=“-1”) 创建ReadOnlyCollection <IDisposable> 来自例如列表 <FileStream> - Create ReadOnlyCollection<IDisposable> from e.g. List<FileStream> 如何在我的代码中处理IDisposable? - How can I dispose the IDisposable in my code? 设置文化时,我可以覆盖日期格式,例如毫米/日/年? - When setting Culture, can I override the date format to e.g. mm/dd/yyyy? 当用户输入特殊字符时如何显示菜单或下拉列表,例如 % - How can I have display a menu or dropdown list when user enters a special character e.g. % 使用 ClickOnce 发布时如何获取状态信息(例如,7/30 已上传)? - How can I get status information (e.g., 7/30 uploaded) when publishing using ClickOnce? 在 WindowsFormsHost 控件中更新 WinForms Chart 时,WPF WebBrowser 不更新 - WPF WebBrowser not updating when WinForms Chart is updated in WindowsFormsHost control 嵌入 WPF ScrollViewer 时,WindowsFormsHost 控件被过度剪辑 - WindowsFormsHost control is being overclipped when embedded inside a WPF ScrollViewer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM