Memory leaks MVVM Light Toolkit Silverlight中带有交互触发器的内存泄漏

Memory leaks MVVM Light Toolkit Silverlight中带有交互触发器的内存泄漏,memory-leaks,mvvm-light,silverlight-5.0,Memory Leaks,Mvvm Light,Silverlight 5.0,你好!! 我在MVVM Light工具箱中使用交互触发器时发现内存泄漏。 我使用这个xaml <UserControl x:Class="MemoryLeakTest.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns

你好!! 我在MVVM Light工具箱中使用交互触发器时发现内存泄漏。 我使用这个xaml

<UserControl x:Class="MemoryLeakTest.MainPage"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:ignore="http://www.ignore.com"
         mc:Ignorable="d ignore"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            xmlns:mvvm="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL5"
         x:Name="control"
         DataContext="{Binding Main, Source={StaticResource Locator}}">

<Grid x:Name="LayoutRoot">

    <ItemsControl ItemsSource="{Binding LeakObjects}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border  Width="300" BorderThickness="6" BorderBrush="BlueViolet" CornerRadius="3">
                    <Grid Background="{Binding ColorBrush}" >
                        <StackPanel>
                            <Button Command="{Binding ElementName=control, Path=DataContext.Command}"  Width="100" Height="40" Content="Tryck!">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <mvvm:EventToCommand Command="{Binding ElementName=control, Path=DataContext.Command}" CommandParameter="{Binding}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Button>
                            <TextBlock Text="{Binding Text}"/>
                        </StackPanel>
                    </Grid>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

然后我重新绑定列表对象,以便创建新项。 按钮和文本块之类的旧项(xaml)仍在内存中,并且不存在

如果我写

<Button Command="{Binding ElementName=control, Path=DataContext.Command}"  Width="100" Height="40" Content="Press!"/>

并且使用buttons命令参数没有内存泄漏,但是如果我使用

<i:Interaction.Triggers>
     <i:EventTrigger EventName="Click">
          <mvvm:EventToCommand Command="{Binding ElementName=control, Path=DataContext.Command}" CommandParameter="{Binding}"/>
     </i:EventTrigger>
 </i:Interaction.Triggers>

有个大漏洞

问题是网格上没有命令参数等

链接中的项目有一个非常简单的项目来演示这个问题

有没有办法避免内存泄漏?也许我用错了


我必须找到一种方法来修复这个问题,因为这个内存泄漏遍布我们的应用程序。

我也经历过这个泄漏。我解决了这个问题,没有使用EventToCommand,而是使用普通的事件处理程序,并在页面代码隐藏中从这些方法调用命令。虽然没有那么干净,但它可以正常工作,页面按预期进行垃圾收集。
或者您可以使用InvokeCommandAction来代替,这对我很有用。