XAML和WindowsFormsHost的内存泄漏 [英] Memory leak with XAML and WindowsFormsHost
问题描述
首先,我向您展示我所拥有的,然后再解释我的问题.
我有一个代码,可以根据属性布局"在样式之间进行切换,可以是"Layout1"或"Layout2".
First I show you what I have and than I explain my problem.
I have a code where I can switch between styles depending on the property "Layout", which can be "Layout1" or "Layout2".
<Grid>
<ContentControl Style="{Binding Layout}">
</ContentControl>
</Grid>
以下是样式定义:
Here are the Style definitions:
<Style x:Key="Layout1" TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
.....
<ContentControl Style="{StaticResource myWindow}">
</ContentControl>
.....
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="Layout2" TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
.....
<ContentControl Style="{StaticResource myWindow}">
</ContentControl>
.....
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
每个样式都使用静态资源"myWindow".在myWindow中,一个类myControl和myControl使用WindowsFormsControl myControl5.
Each Style use the static ressource "myWindow". In myWindow is a class myControl and myControl use a WindowsFormsControl myControl5.
<Style x:Key="myWindow" TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<Grid >
<local:myControl Margin="0,0,0,0"
</local:myControl>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type local:myControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:myControl}">
<WindowsFormsHost x:Name="PART_WindowsFormsHost">
<cbh:myControl5></cbh:myControl5>
</WindowsFormsHost>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在myControl类中,我获得了myControl5的句柄,因此可以使用它来添加事件处理程序.
In the class myControl I get the handle to myControl5 so I can use it to adding event handler.
public class myControl : Control
{
public override void OnApplyTemplate ()
{
base.OnApplyTemplate ();
this.windowsFormsHost = GetTemplateChild ("PART_WindowsFormsHost") as WindowsFormsHost;
myControl5 mc5 = this.InnerControl;
mc5.GotFocus += new EventHandler(InnerControl_GotFocus);
.....
}
}
现在我的问题是:
每次更改样式布局时,我都会看到WPF创建了一个新的myControl,但从未发布过.因此,内存使用量每次都会增加.
我希望说明如此有用,您可以说"Natch,您忘记了……"或类似的内容.
Now my problem:
Each time the Style Layout change, I see that WPF creates a new myControl but never release it. So the memory usage increase each time.
I hope the explaining is so useful, that you can say "Natch, you forgot ..." or something like that.
推荐答案
mc5.GotFocus += new EventHandler(InnerControl_GotFocus);
此事件与本地上下文相关,我想您不会释放此引用.请删除此钩子以及所有其他存在的钩子,如下所示.
进行全局引用并在下次调用Apply模板时取消挂接事件.
This event is hooked with in the local context, and I guess you dont release this reference. Please remove this hook and any others if exists as follows.
make a global reference and unhook the event the next time apply template is invoked.
myControl5 mc5 = null;
public override void OnApplyTemplate ()
{
if(mc5!=null)
mc5.GotFocus += new EventHandler(InnerControl_GotFocus);
base.OnApplyTemplate ();
this.windowsFormsHost = GetTemplateChild ("PART_WindowsFormsHost") as WindowsFormsHost;
mc5 = this.InnerControl;
mc5.GotFocus += new EventHandler(InnerControl_GotFocus);
}
希望对您有帮助!
Hope it helps!!!
这篇关于XAML和WindowsFormsHost的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!