在Windows 8应用程序中,我使用了ItemsControl和ItemTemplate,其中包含事件:
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="Tap">
<command:EventToCommand Command="{Binding Mode=OneWay, Path=DataContext.NavigateToNextPage, Source={StaticResource Context}}" CommandParameter="{Binding}" />
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>-->上下文:
<ContentControl x:Key="Context" Content="{Binding}" />RelayCommand:
public RelayCommand<MyItem> NavigateToNextPageCommand
{
get { return _navigateToNextPageCommand ?? (_navigateToNextPageCommand = new RelayCommand<MyItem>(NavigateToNextPage)); }
}ItemsControl的定义是:
<ItemsControl Grid.Row="2" ItemsSource="{Binding DepositsItems}">DepositsItems是一个包含大约200个元素的列表,我有时会重新加载它。几次重新加载之后,内存泄漏,应用程序关闭。我找到了发生的原因。当我删除Tap事件时,一切都正常。我认为该命令保存对item的引用,GC不释放内存。
有没有办法从项目中“解除绑定”命令?我关心MVVM模式。
我发现了这个:https://atomaras.wordpress.com/2012/04/23/solving-mvvmlights-eventtocommand-memory-leak-wp7/,但是它不起作用。有没有更简单的解决办法?
固定
我通过将EventToCommand更改为InvokeCommandAction来修复它。
发布于 2016-01-04 12:39:33
我通过将EventToCommand更改为InvokeCommandAction.来修复它
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="Tap">
<command:InvokeCommandAction Command="{Binding Mode=OneWay, Path=DataContext.NavigateToNextPage, Source={StaticResource Context}}" CommandParameter="{Binding}" />
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>https://stackoverflow.com/questions/34431934
复制相似问题