如果在将要处理的 Windows 表单中使用,WPF 用户控件是否会被处理?

Will the WPF user Control be Disposed if used inside a Windows Form that will be disposed?

所以我只是 learned 我们可以使用 ElementHost 控件将 WPF UserControl 放入 windows Form。如果那个 windows 表单控件被处理掉了,WPF 用户控件也会被处理掉吗?

如果你的 WPF UserControlIDisposable 答案是肯定的,否则不是。

在承载 WPF UserControlElementHost class 的 Dispose 方法的源代码中,您可以看到:

IDisposable child = this.Child as IDisposable;
if (child != null)
{
    child.Dispose();
}

这意味着 Child 将被处理,如果它是 IDisposable

备注

WPF 不依赖 IDisposable 接口进行资源清理。但是由于 UserControl 将在支持 IDisposable 模式的 ElementHost 控件中的 Windows 表单项目中使用,因此如果需要,您可以依赖 IDisposable 模式执行一些资源清理。但是如果是WPF项目,你应该使用WPF机制来进行资源清理。